Danielle Danielle - 2 months ago 13
PHP Question

Difference in php validation

I have two forms with validations for both. However, one works and the other doesn't. I would like to know what's the difference between these two validations why only one works. I've simply included the sections of the respective forms that are relevant to the problem. If you require additional lines of code, let me know.

Edit: When I say it doesn't work I mean that if I submit an empty field for the first one, the error message comes up i.e. "Please enter a valid name.". If I should submit an empty field for the second one, the "please insert username" doesn't show.

The one that works.

Form snippets:

echo "<form action =\"valid.php\" method = \"POST\">
<td><input type = \"text\" name = \"name\" value = \"$name\" placeholder = \"Enter name here.\"/></td>
<td><input type = \"submit\" name = \"submit\" value = \"Send Info\"/> </td>


Validation code:

<?php
if (isset($_POST['submit']))
{$name = $_POST['name'];

if($name == NULL)
{echo "Please enter a valid name.</br>";}
}
?>


The one that doesn't work:

Form Snippet:

<form action="validate.php" method="POST">';
echo 'Username: <input type="text" name="username" value="$username" > <br>';
echo' <input type="submit" name="submit" value="submit">';


Validation code:

<?php
if (isset($_POST['submit'])) {
$username = $_POST['username'];
if ($username == NULL){
echo "please insert username";
}
}
?>

Answer
echo "Username: <input type='text' name='username' value='$username' >   <br>";

echo "<input type='submit' name='submit' value='submit'>";

Try using " outer quotations as ' for inner quotations