Else statment viewed before anything else...
#1
Posted 10 March 2010 - 11:39 AM
ever I run a script that does the following...
I have a form on a page which has a form with a text field which when submitted would echo something like
'Thanks for your deposit'
the else conditional would say 'The amount you entered was invalid'
Now when I run this script, before any amount has been submitted id get the 'else' conditional echoing on the page, above the form.
Without seeing my exact script chan this be rectified by knowing the above?
#2
Posted 10 March 2010 - 11:43 AM
#3
Posted 10 March 2010 - 11:45 AM
if(isset($_POST)) {
// .. Your php code here ..
}That should solve it (change $_POST to $_GET if you're using the GET method with your form)
#4
Posted 10 March 2010 - 11:52 AM
but, to be sure I best get off my lazy arse and grab the laptop out the car...its further than it sounds.
#5
Posted 10 March 2010 - 11:53 AM
If you post the code it will help that's for sure. It was only a guess
#6
Posted 10 March 2010 - 12:24 PM
heres the problems Ive got
1. When you dont fill out a textfield. ie leave blank no errors are displayed, UNLESS the password fields dont match.
2. I get the error message
Warning: Invalid arguement supplied Foreach() in ...Line 111
3. As I originally posted I get the Error message of 'Error, the following errors occured' before anything is submitted. And ironically it doesnt even list the errors array..
These are the three main ones
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" media="screen" type="text/css" />
<title>Untitled Document</title>
</head>
<body>
<?php
$page_title ='register.php';
//Initialize an errors array...
$errors = array ();
//Check First name
if (empty($_POST['first_name']))
{
$errors [] = 'You forgot to enter a First name';
}
else
{
$fn = trim($_POST['first_name']);
}
//Check Last Name
if (empty($_POST['last_name']))
{
$errors [] = 'You forgot to enter a Last name';
}
else
{
$ln = trim($_POST['last_name']);
}
// Check Email address
if (empty($_POST['email']))
{
$errors [] = 'You forgot to enter an Email Address';
}
else
{
$e = trim($_POST['email']);
}
// Check Password
if (!empty($_POST['pass1']))
{
if ($_POST['pass1'] != $_POST['pass2'])
{
$errors [] = 'Your passwords didnt match';
}
else
{
$p = trim ($_POST['pass1']);
}
}
else
{
$errors ="You forgot to enter a password";
}
if (empty ($errors))
{
//Register the user in the database
require_once
('mysqli_connect.php');
//make the query
$q = "INSERT INTO users (first_name, last_name, email, pass) VALUES ('$first_name', '$last_name', '$email', SHA1('$pass') )";
$r = mysqli_query ($dbc, $q) ;
//The above code runs the query
if ($r) {
echo " Thanks for registering, ";
}
else
{
echo '<p class="errors"> you could not be registered due to a system error</p>';
}
mysqli_close($dbc);
exit();
}
else {
echo '<h1>Error</h1>
<p class="errors"> The following errors occured:<br/>';
foreach
($errors as $msg)
{
echo "-$msg <br/>\n";
}
}
//Below is the fields for the form
?>
<h1>Register</h1>
<form action="register.php" method="post" class="myForm">
First Name:<input type="text" name="first_name" size="15" maxlength="20" value="
<?php
if (isset($_POST['first_name']))
echo $_POST['first_name'];
?> " /><br />
Last Name:<input type="text" name="last_name" size="15" maxlength="40" value="
<?php
if (isset($_POST['last_name']))
echo $_POST['last_name'];
?> " /><br />
Email:<input type="text" name="email" size="20" maxlength="80" value="
<?php
if (isset($_POST['email']))
echo $_POST['email'];
?> " />
<br>
Password: <input type="password" name="pass1" size="10" maxlength="20" />
<br />
Confirm Password <input type="password" name="pass2" size="10" maxlength="20" /><br />
<input type="submit" name="submit" value="register" /><br />
<input type="hidden" name="submitted" value="TRUE" /><br />
</form>
</body>
</html>
Now I want you to know Ive not come running to you guys, ive spent ages changing the code trying to work this out, but I have conceeded, I need some guidance...
After all I think you all owe me, before I came to this site I had a passing interesst in webdesign, then someone suggested I buy a book and have subsequently spent the last month pulling my hair out.
#7
Posted 10 March 2010 - 12:33 PM
#8
Posted 10 March 2010 - 01:26 PM
Thats good to know as I thought it looked a little rubbish, Im following a book. Which although may not be the best practice, it has taught me a lot about PHP.
Still some confusing elements though
eg. make sence of this part of the code for me
foreach ($errors as $msg) {
echo "-$msg <br/>\n"; }especialy the logic behind "-$msg"
Cheers
#9
Posted 10 March 2010 - 01:35 PM
Adam, on 10 March 2010 - 01:26 PM, said:
Thats good to know as I thought it looked a little rubbish, Im following a book. Which although may not be the best practice, it has taught me a lot about PHP.
Still some confusing elements though
eg. make sence of this part of the code for me
foreach ($errors as $msg) {
echo "-$msg <br/>\n"; }especialy the logic behind "-$msg"
Cheers
You're just outputting each element of an array. When you're checking your posted data if it meets a certain condition you add a element to the $errors array. the "-$msg" is the array element and simply produces a list of errors with a dash before each line. E.g.
if (empty($_POST['last_name']))
{
//add a element to your errors array
$errors [] = 'You forgot to enter a Last name';
}To see this more easily, add print_r($errors) at the end of your script. Enter some invalid data to see the content of the $errors array.
#10
Posted 10 March 2010 - 01:38 PM
Out of interest, what book are you following?
#11
Posted 10 March 2010 - 01:41 PM
Thanks for the reply.
After reading the above guys comments about rewritting the script, I want some further advice before I go back to the drawing board.
Why exactly am I going wrong in this script? Whats making it look ugly and not perform correctly?
I understand your not here to write it for me but if I start again I dont want to make the same mistakes.
Thanks
Adam
#12
Posted 10 March 2010 - 01:43 PM
Jay Gilford, on 10 March 2010 - 01:38 PM, said:
Out of interest, what book are you following?
PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (Visual QuickPro Guides)
Advised by one of the other guys on here.
A lot of it makes sence, but does not always work the way he intend...more my fault than his I imagine.
#13
Posted 10 March 2010 - 02:04 PM
$errors = array();
//Check that form has been submitted
if(isset($_POST)) {
// Loop through all posted form variables
foreach($_POST as $k => $v) {
// Trim the variable
$_POST[$k] = trim($_POST[$k]);
// If it's empty add error "You forgot to enter field_name_here"
if(empty($_POST[$k])) $errors[] = 'You forgot to enter '.$k;
}
//Any additional error checking such as password check
if($_POST['pass1'] != $_POST['pass2']) {
$errors[] = 'Passwords do not match';
}
// Check for error count here and insert into database if none
if(empty($errors)) {
// Put mysql insert code here
}
} Also just so you know, the isnert query you have is wrong and won't actually insert anything since you have $fn when defining the first name and $firstname in the query. Its the same for last name too
One final note. When creating queries it's much better to use something like sprintf to build your query with your variables rather than just sticking them into the string directly. If you use the mressf function I created it will also run each through the myqsl_real_escape_string function too to prevent mysql injection attacks
- ← Bread crumb not showing
- Server Side (PHP, Databases, ASP.NET, etc)
- i want my dropdown to always select the value from database as defualt. →
Help
















