Prev | Current Page 418 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


Listing 13-1. Validating Form Data in a Function (subscribe.php)
// Function used to check e-mail syntax
function validateEmail($email)
CHAPTER 13 ?–  FORMS 353
{
// Create the e-mail validation regular expression
$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)
(\.[a-z0-9-]+)*(\.[a-z]{2,6})$";
// Validate the syntax
if (eregi($regexp, $email)) return 1;
else return 0;
}
// Has the form been submitted?
if (isset($_POST['submit']))
{
$name = htmlentities($_POST['name']);
$email = htmlentities($_POST['email']);
printf("Hi %s
", $name);
if (validateEmail($email))
printf("The address %s is valid!", $email);
else
printf("The address %s is invalid!", $email);
}
?>


Name:




E-mail Address:





354 CHAPTER 13 ?–  FORMS
Working with Multivalued Form Components
Multivalued form components such as checkboxes and multiple-select boxes greatly
enhance your Web-based data-collection capabilities because they enable the user to
simultaneously select multiple values for a given form item. For example, consider a
form used to gauge a user??™s computer-related interests.


Pages:
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430