Forum Moderators: coopster

Message Too Old, No Replies

How to make a Proper If/Then Statement

Clean mine up a bit?

         

naitsirhc26

5:23 pm on Aug 1, 2007 (gmt 0)

10+ Year Member



Hello,

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!

WesleyC

5:41 pm on Aug 1, 2007 (gmt 0)

10+ Year Member



I see a couple problems here.

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!";
}
?>

henry0

5:52 pm on Aug 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I won't do it in bulk
you should test them one at a time

and test also if the sumitted input is the expected one.

naitsirhc26

7:30 pm on Aug 1, 2007 (gmt 0)

10+ Year Member



Thank you very much for the tips. I will start writing all of my code regarding register_globals OFF.

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!

jatar_k

7:34 pm on Aug 1, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you have to replace the ¦ char with a real unbroken pipe character

WebmasterWorld breaks pipes

naitsirhc26

7:48 am on Aug 2, 2007 (gmt 0)

10+ Year Member



Hey thanks, I learned something today!

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

misterjonez

8:02 am on Aug 2, 2007 (gmt 0)

10+ Year Member



hey try this without using $_GET or $_POST...

$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

?>

jatar_k

12:24 pm on Aug 2, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you shouldn't use extract, too open to abuse

you should grab values from $_POST one by one, so that anything extra is not imported into the variable table

WesleyC

12:55 pm on Aug 2, 2007 (gmt 0)

10+ Year Member



Yes, this is also the reasoning behind turning off register_globals.