The point is I find it easy to learn by doing "practical projects."
I have been learning how to process HTML forms with PHP.
The problem is every book or tutorial I have read or encountered fall short on validation.
I'm wondering how I can learn to validate Radio Buttons and Select List Menu.
I have managed to create validation for all other fields but have no clue as to how I can get validation for Radio Buttons and List Menu.
I would also like an error message echoed when the user does not click a button or make a selection and try to submit the form.
I would appreciate any help.
AI
This is the Radio Button Section of the form:
Code: Select all
<div class="system">
<label for="operatingSystem">Operating System.</label>
</div>
<div class="operatingSystem">
<input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Windows Vista" /><span class="radioSpacer">Windows Vista</span>
<input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Windows XP" /><span class="radioSpacer">Windows XP</span>
<input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Mac OSX" /><span class="radioSpacer">Mac</span>
<input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Linux" /><span class="radioSpacer">Linux</span>
</div>
Code: Select all
<?php
session_start(); // At the very top
// process the email
if (array_key_exists('send', $_POST)) {
$to = 'myemail.com'; // use your own email address
$subject = 'Technical Support Inquiry';
// list expected fields
$expected = array('firstName', 'lastName', 'email', 'phoneNumber', 'operatingSyatem', 'productSelection', 'serialNumber', 'reportProblem');
// set required fields
$required = array('firstName', 'lastName', 'email', 'operatingSyatem', 'productSelection', 'serialNumber', 'reportProblem');
// create empty array for any missing fields
$missing = array();
// assume that there is nothing suspect
$suspect = false;
// create a pattern to locate suspect phrases
$pattern = '/Content-Type:|Bcc:|Cc:/i';
// function to check for suspect phrases
function isSuspect($val, $pattern, &$suspect) {
// if the variable is an array, loop through each element
// and pass it recursively back to the same function
if (is_array($val)) {
foreach ($val as $item) {
isSuspect($item, $pattern, $suspect);
}
}
else {
// if one of the suspect phrases is found, set Boolean to true
if (preg_match($pattern, $val)) {
$suspect = true;
}
}
}
// check the $_POST array and any sub-arrays for suspect content
isSuspect($_POST, $pattern, $suspect);
if ($suspect) {
$mailSent = false;
unset($missing);
}
else {
// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
}
// validate the email address
if (!empty($email)) {
// regex to ensure no illegal characters in email address
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
// reject the email address if it doesn't match
if (!preg_match($checkEmail, $email)) {
array_push($missing, 'email');
}
}
// go ahead only if not suspect and all required fields OK
if (!$suspect && empty($missing)) {
// set default values for variables that might not exist
$subscribe = isset($subscribe) ? $subscribe : 'Nothing selected';
$interests = isset($interests) ? $interests : array('None selected');
// build the message
$message = "First Name: $firstName\n\n";
$message .= "Last Name: $lastName\n\n";
$message .= "Email: $email\n\n";
$message .= "Phone Number: $phoneNumber\n\n";
$message .= "Operating System: $operatingSystem\n\n";
$message .= "Trouble Product: $productSelection\n\n";
$message .= "Serial Number: $serialNumber\n\n";
$message .= "Trouble Details: $reportProblem\n\n";
// limit line length to 70 characters
$message = wordwrap($message, 70);
// send it
$mailSent = mail($to, $subject, $message, 'From: '.$firstName.' '.$lastName.' <'.$email.'>' );
if ($mailSent) {
//redirect the page with a fully qualified URL
$_SESSION['firstName'] = $firstName;
header('Location: http://www.patrickjudson.com/confirmation.php');
exit;
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="tech_support.css" rel="stylesheet" type="text/css" media="screen" />
<style type="text/css">
<!--
.radioSpacer {
padding-right: 8px;
}
.style1 {
color: #030303;
font-size: 11px;
width: 176px;
display: block;
float: left;
clear: none;
padding-top: 22px;
}
-->
</style>
</head>
<body>
<div id="formWrapper">
<p class="formTitle">Technical Support - Please fill out form completely.</p>
<form id="support" name="tech_support" class="techsupport_form" method="post" action="">
<!--PERSONAL INFORMATION-->
<div class="personalInfo">
<div class="name_fieldWrapper">
<label for="firstName">First Name: <?php
if (isset($missing) && in_array('firstName', $missing)) { ?>
<span class="warning">Required!</span><?php }
?>
</label>
<input type="text" name="firstName" id="first_name" class="name_inputControl"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['firstName']).'"';} ?> />
</div>
<div class="name_fieldWrapper">
<label for="lastName">Last Name: <?php
if (isset($missing) && in_array('lastName', $missing)) { ?>
<span class="warning">Required!</span><?php }
?></label>
<input type="text" name="lastName" id="last_name" class="name_inputControl"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['lastName']).'"';} ?> />
</div>
<div class="email_fieldWrapper">
<label for="email">E-mail: <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Required!</span><?php } ?>
</label>
<input type="text" name="email" id="email" class="email_inputControl"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email']).'"';} ?> />
</div>
<div class="phone_fieldWrapper">
<label for="phoneNumber">Phone Number:
<?php
$error = '';
if(array_key_exists('phoneNumber', $_POST))
{
if(!preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/', $_POST['phoneNumber']))
{
$error = 'Invalid Number!';
}
}
?>
<span class="warning"> <?php echo $error; ?></span></label>
<input name="phoneNumber" type="text" class="phoneNumber_inputControl" id="phoneNumber"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['phoneNumber']).'"';} ?> />
</div>
<span class="style1">U.S. Only. Eg: 111-111-111</span></div>
<!--##############################NEWS LETTER################################-->
<div class="system">
<label for="operatingSystem">Operating System.</label>
</div>
<div class="operatingSystem">
<input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Windows Vista" /><span class="radioSpacer">Windows Vista</span>
<input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Windows XP" /><span class="radioSpacer">Windows XP</span>
<input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Mac OSX" /><span class="radioSpacer">Mac</span>
<input name="operatingSystem" type="radio" class="system_checkBox" id="userSystem" value="Linux" /><span class="radioSpacer">Linux</span>
</div>
<!--###################################pPROBLEM REPORT SECTION#############################-->
<div class="problemProduct">
<label for="productSelection"><span class="product_label">Product Name.</span></label>
<select name="productSelection" id="products" class="selection">
<option value="None">-------------Select a product----------</option>
<option value="Everex DVD Burner">Everex DVD Burner</option>
<option value="Vidia DVD Burner">Vidia DVD Burner</option>
<option value="Excerion Super Drive">Excerion Super Drive</option>
<option value="Maxille Optical Multi Burner">Maxille Optical Multi Burner</option>
<option value="Pavilion HD Drives">Pavilion HD Drives</option>
</select>
</div>
<!--###################################SERIAL NUMBER#############################-->
<div class="problemProduct">
<?php
$serialNumbers = array('AB2468101214','123456');
$error = (in_array($_POST['serialNumber'], $serialNumbers) && !empty($_POST['serialNumber']))? null : 'A valid Product Serail Number Required!';
?>
<label for="serialNumber"><span class="product_label">Serial Number.</span></label>
<input type="text" name="serialNumber" id="serial" class="serial_numberField"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['serialNumber']).'"';} ?> />
<span class="warning"> <?php echo $error; ?></span>
</div>
<!--##########################COMMENTS/MESSAGE BOX########################-->
<div class="commentBox">
<label for="reportProblem">Please explain in detail the problem you are experiencing.
<?php
if (isset($missing) && in_array('reportProblem', $missing)) { ?>
<span class="warning">Required!</span> <?php } ?>
</label>
<textarea name="reportProblem" id="report" cols="56" rows="6" class="textArea_inputControl">
<?php
if (isset($missing)) {
echo htmlentities($_POST['reportProblem']);
} ?>
</textarea>
</div>
<div class="submitForm">
<input name="send" id="submit" class="submitButton" type="submit" value="Report Problem" />
<input name="reset" id="cancel" class="submitButton" type="reset" value="Reset Form" />
</div>
<br class="gForce" />
</form>
</div>
</body>
</html>