Forum Moderators: coopster
I am trying to grasp the concept of an If/Then Statement, and I will show the following code, which is my own statement that I created, along with the HTML form that is being processed. But why is it that the output is ALWAYS the IF part, even if I put in the wrong password, or don't fill in all of the fields? I would like to know what the most current and proper way to write an IF/THEN statement is:
<form name="form1" method="post" action="test.php">
<p>Name
<input name="name" type="text" id="name">
</p>
<p>Email
<input name="email" type="text" id="email">
</p>
<p>Password
<input name="password" type="text" id="password">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
And here is my PHP Processor:
<?php
if ($name = "" ¦¦ $email = "" ¦¦ $password = "") {
echo "Please fill in all of the fields.";
} else {
echo "Thank you for completing the form.";
}
?>
<br><br>
<?php
if ($password = "password") {
echo "Thank you for using the correct password.";
} else {
echo "You did not use the correct password!";
}
?>
Thanks!
The first is that you're using an assignment operator (=) in your if statements instead of the conventional operator (==).
if ( $name == "" )
will return true only if $name is equal to "", while
if ( $name = "" )
will always return true, and will assign the value of "" to $name.
Also, do you have register_globals enabled? If you do, turn it off now. It's a horrible security risk, and you might as well get used to programming without it now, rather than learning to rely on it--especially since PHP 6 removes the setting entirely.
Without register_globals, you will need to use $_POST["form_element_name"] instead of $form_element_name to access the contents of a variable from a form.
Your corrected PHP code:
<?php
if ($_POST["name"] == "" ¦¦ $_POST["email"] == "" ¦¦ $_POST["password"] == "") {
echo "Please fill in all of the fields.";
} else {
echo "Thank you for completing the form.";
}
?>
<br><br>
<?php
if ($_POST["password"] == "password") {
echo "Thank you for using the correct password.";
} else {
echo "You did not use the correct password!";
}
?>
When I run this new program, I get this response:
Parse error: parse error, unexpected T_STRING
So the error is in the second line of this set of code:
<?php
if ($_POST["name"] == "" ¦¦ $_POST["email"] == "" ¦¦ $_POST["password"] == "") {
echo "Please fill in all of the fields.";
} else {
echo "Thank you for completing the form.";
}
?>
Is there a mistake in the second line of that code? I can't seem to find anything that would be wrong.
Thanks again!
Well I am very excited about PHP, I can tell you that! After today I have a working knowledge of sending emails through PHP, the IF/THEN statement, ECHO $_POST, and a bunch of other things.
After today I was able to make a script that made little MadLibs, a password protected page, and an email web form.
I have used PHP for a long time in my site, but it has all been free PHP that is already made for you. Open Source. But now, I am really looking forward to when I will have enough knowledge to develop my own applications for my site.
PHP is AWESOME!
Cheerful Trails,
Christian
$password, $username :) this works great!
<?php //Grab GLOBALS
if (!empty($_GET)) {
extract($_GET, EXTR_OVERWRITE);
} else if (!empty($HTTP_GET_VARS)) {
extract($HTTP_GET_VARS, EXTR_OVERWRITE);
} // end if
if (!empty($_POST)) {
extract($_POST, EXTR_OVERWRITE);
} else if (!empty($HTTP_POST_VARS)) {
extract($HTTP_POST_VARS, EXTR_OVERWRITE);
} // end if
?>