You want to make sure a value has been supplied for a form element, i.e. a field is not left blank.
Use strlen() to test the element in $_GET or $_POST.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php function display_form() { echo <<<HTML <form action = "$_SERVER[SCRIPT_NAME]" method="post"> Enter first name <input type="text" name="first_name" /> <input type="submit" value="Submit" /> </form> HTML; } // Make sure that the first_name field exists before checking its length if (isset($_POST['first_name']) && strlen($_POST['first_name']) ) { echo 'Hello, '.$_POST['first_name'], '<br />'; } else { echo "You must enter your first name."; display_form(); } ?> </p> </body> </html> |
You want to make sure that the length of the input field has a limit.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php function display_form() { echo <<<HTML <form action = "$_SERVER[SCRIPT_NAME]" method="post"> Enter first name <input type="text" name="first_name" /> <input type="submit" value="Submit" /> </form> HTML; } // Make sure that the first_name field exists and has less than 10 characters if ((isset($_POST['first_name']) && strlen($_POST['first_name'])<=10 )) { echo 'Hello, '.$_POST['first_name']; } else { echo "Your first name does not exceed 10 characters."; display_form(); } ?> </p> </body> </html> |