hi,
I have used Ajax validation for my form, in my form there are two radio buttons both has separate fields and my requirement is to validate only the selected radio button fields but its validating for both radio buttons, can u please suggest what to do?
shanthi.juturu
0
Newbie Poster
<?php
// set the user error handler method to be error_handler
set_error_handler('error_handler', E_ALL);
// error handler function
function error_handler($errNo, $errStr, $errFile, $errLine)
{
// clear any output that has already been generated
if(ob_get_length()) ob_clean();
// output the error message
$error_message = 'ERRNO: ' . $errNo . chr(10) .
'TEXT: ' . $errStr . chr(10) .
'LOCATION: ' . $errFile .
', line ' . $errLine;
echo $error_message;
// prevent processing any more PHP scripts
exit;
}
?>
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
<?php
require_once ('index_top.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Practical AJAX: Form Validation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="validate.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="validate.js"></script>
</head>
<body>
<fieldset>
<legend class="txtFormLegend">New User Registration Form</legend>
<br />
<form name="frmRegistration" method="post" action="validate.php?validationType=php">
<table border=0 id="form" style="padding:10px;">
<tr>
<td colspan="2" align=left valign="top" style="padding-right:5px; color:#000;"><span class="style1">Please Use the following form to provide information about your promotional bag</span></td>
</tr>
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />
<?php
$checked1= $checked='';
if($_REQUEST[stand_custm]==2)
{
$checked=' CHECKED';
}else{
$checked='';
}
if($_REQUEST[stand_custm]==1)
{
$checked1=' CHECKED';
}else{
$checked1='';
}
?>
<tr>
<td colspan="2" align=left valign="top" style="padding-right:5px; color:#000;"> <table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr>
<td width="300" align="left" valign="top" class="pageContent" scope="col">
<input checked="checked" type='radio' name='stand_custm' id='radio' value='1' onclick='stand_custms(this.value)' <?php echo $checked1;?> />
Select from our standard bag sizes/quantities</td>
<td align="left" valign="top" class="pageContent" scope="col"><input type='radio' name='stand_custm' id='stand_custm' value='2' onclick='stand_custms(this.value)' <?php echo $checked;?> />
Customize your bag <span class="style8">*</span></td>
</tr>
</table> </td>
</tr>
<tr>
<td colspan="2" align=left valign="top" style="padding-right:5px; color:#000;"> <div id='standa' style='display:block'>
<table width="500" bgcolor="#EAF1E7" class="pageContent">
<tr>
<td width="166" align="left" valign="top" class="pageContent">Bag Size in inches:<br>
(height X width X depth)</td>
<td width="422" class="pageContent">
<!--<select name='bag_size'>
<option>Select Bag Size</option>
<option value='18 X 16 X 6'>18 X 16 X 6</option>
<option value='17 X 14 X 5'>17 X 14 X 5</option>
<option value='13 X 17 X 8'>13 X 17 X 8</option>
</select>
<span class="style8">*</span>
-->
<select name="selSize" id="selSize"
onblur="validate(this.value, this.id)">
<?php buildOptions($sizeOptions,
$_SESSION['values']['selSize']); ?>
</select>
<?php /*?> <span id="selSizeFailed"
class="<?php echo $_SESSION['errors']['selSize'] ?>">
Please select Bag Size.
</span> <?php */?>
</td>
</tr>
<tr>
<td class="pageContent">Standard Quantity:</td>
<td class="pageContent"><!--<select name='bag_quantity'>
<option>Select Quantity</option>
<option value='80'>80</option>
<option value='160'>160</option>
<option value='240'>240</option>
<option value='320'>320</option>
<option value='400'>400</option>
<option value='480'>480</option>
<option value='560'>560</option>
<option value='640'>640</option>
<option value='720'>720</option>
<option value='800'>800</option>
<option value='880'>880</option>
<option value='960'>960</option>
</select>
<span class="style8"> * </span>-->
<select name="selQuantity" id="selQuantity"
onblur="validate(this.value, this.id)">
<?php buildOptions($quantityOptions,
$_SESSION['values']['selQuantity']); ?>
</select>
<?php /*?> <span id="selQuantityFailed"
class="<?php echo $_SESSION['errors']['selQuantity'] ?>">
Please select Bag Quantity.
</span> <?php */?> <br /> </td>
</tr>
<tr>
<td class="pageContent"> </td>
<td class="pageContent"> </td>
</tr>
</table>
</div>
<div id='customis' style='display:none'>
<table width="500" class="pageContent" style="background-color:#EAF1E7">
<tr>
<td class="pageContent" valign="middle"><strong>Enter Bag Size</strong></td>
<td> </td>
</tr>
<tr>
<td class="pageContent" align="right">Height:</td><td > <input id="txtHeight" name="txtHeight" type="text"
onblur="validate(this.value, this.id)"
value="<?php echo $_SESSION['values']['txtHeight'] ?>" />
<?php /*?> <span id="txtHeightFailed"
class="<?php echo $_SESSION['errors']['txtHeight'] ?>">
Please insert a valid 2 Digit Height Value
</span> <?php */?> </td></tr>
<tr><td class="pageContent" align="right">Width:</td><td><input id="txtWidth" name="txtWidth" type="text"
onblur="validate(this.value, this.id)"
value="<?php echo $_SESSION['values']['txtWidth'] ?>" />
<?php /*?><span id="txtWidthFailed"
class="<?php echo $_SESSION['errors']['txtWidth'] ?>">
Please insert a valid 2 Digit Width Value
</span> <?php */?> </td></tr>
<tr><td class="pageContent" align="right">Depth:</td><td><input id="txtDepth" name="txtDepth" type="text"
onblur="validate(this.value, this.id)"
value="<?php echo $_SESSION['values']['txtDepth'] ?>" />
<?php /*?> <span id="txtDepthFailed"
class="<?php echo $_SESSION['errors']['txtDepth'] ?>">
Please insert a valid 2 Digit Depth Value
</span> <?php */?> </td>
</tr>
<tr>
<td class="pageContent" align="right">Quantity:</td><td><input id="txtQuantity" name="txtQuantity" type="text"
onblur="validate(this.value, this.id)"
value="<?php echo $_SESSION['values']['txtQuantity'] ?>" />
<?php /*?> <span id="txtQuantityFailed"
class="<?php echo $_SESSION['errors']['txtQuantity'] ?>">
Please insert a valid 2 Quantity Depth Value
</span><?php */?> </td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td align=right style="padding-right:5px; color:#000;">
Delivary Date </td>
<td align=left>
<input type="text" size="12" id="inputField" /></td>
</tr>
<tr>
<td align=right style="padding-right:5px; color:#000;">
upload your logo file </td>
<td align=left>
<input name="logo" type="file" class="inputtextreg" id="logo" value="Browse" /></td>
</tr>
<tr>
<td align=right style="padding-right:5px; color:#000;">
Will this be a repetitive order? </td>
<td align=left>
<input name="radiobutton" type="radio" value="radiobutton" />yes
<input name="radiobutton1" type="radio" value="radiobutton" />NO </td>
</tr>
<tr>
<td colspan="2" align=left valign="top" style="padding-right:5px; color:#000;"><span class="style1">Please tell us about yourself</span></td>
</tr>
<tr>
<td align=right style="padding-right:5px; color:#000;">
First Name </td>
<td align=left>
<input id="txtName" name="txtName" type="text"
onblur="validate(this.value, this.id)"
value="<?php echo $_SESSION['values']['txtName'] ?>" />
<span id="txtNameFailed"
class="<?php echo $_SESSION['errors']['txtName'] ?>">
Please enter your name.
</span> </td>
</tr>
<tr>
<td align=right style="padding-right:5px; color:#000;">
Company Name </td>
<td align=left>
<input id="txtCompany" name="txtCompany" type="text"
onblur="validate(this.value, this.id)"
value="<?php echo $_SESSION['values']['txtCompany'] ?>" />
<span id="txtCompanyFailed"
class="<?php echo $_SESSION['errors']['txtCompany'] ?>">
Please enter your Company name.
</span> </td>
</tr>
<tr>
<td align=right style="padding-right:5px;color:#000;">
Email Address </td>
<td align=left>
<input id="txtEmail" name="txtEmail" type="text"
onblur="validate(this.value, this.id)"
value="<?php echo $_SESSION['values']['txtEmail'] ?>" />
<span id="txtEmailFailed"
class="<?php echo $_SESSION['errors']['txtEmail'] ?>">
Invalid e-mail address.
</span>
</td>
</tr>
<tr>
<td align=right style="padding-right:5px;color:#000;">
<?php
// enable PHP session
session_start();
// Build HTML <option> tags
function buildOptions($options, $selectedOption)
{ echo $_REQUEST[stand_custm];
foreach ($options as $value => $text)
{
if ($value == $selectedOption)
{
echo '<option value="' . $value .
'" selected="selected">' . $text . '</option>';
}
else
{
echo '<option value="' . $value . '">' . $text . '</option>';
}
}
}
// initialize size options array
$sizeOptions = array("0" => "[Select]",
"1" => "18 X 16 X 6",
"2" => "17 X 14 X 5",
"3" => "13 X 17 X 8");
// initialize quantity options array
$quantityOptions = array("0" => "[Select]",
"1" => "80",
"2" => "160",
"3" => "240",
"4" => "320",
"5" => "400",
"6" => "480",
"7" => "560",
"8" => "640",
"9" => "720",
"10" => "800",
"11" => "880",
"12" => "960");
// initialize gender options array
$genderOptions = array("0" => "[Select]",
"1" => "AP",
"2" => "MP");
// initialize month options array
/*$monthOptions = array("0" => "[Select]",
"1" => "January",
"2" => "February",
"3" => "March",
"4" => "April",
"5" => "May",
"6" => "June",
"7" => "July",
"8" => "August",
"9" => "September",
"10" => "October",
"11" => "November",
"12" => "December");
*/
// initialize some session variables to prevent PHP throwing Notices
if (!isset($_SESSION['values']))
{
if($_REQUEST[stand_custm]==2)
{
$_SESSION['values']['selSize'] = '';
$_SESSION['values']['selQuantity'] = '';
}
else if($_REQUEST[stand_custm]==1)
{
$_SESSION['values']['txtHeight'] = '';
$_SESSION['values']['txtWidth'] = '';
$_SESSION['values']['txtDepth'] = '';
$_SESSION['values']['txtQuantity'] = '';
}
$_SESSION['values']['txtName'] = '';
$_SESSION['values']['txtCompany'] = '';
/* $_SESSION['values']['selGender'] = '';
$_SESSION['values']['selBthMonth'] = '';
$_SESSION['values']['txtBthDay'] = '';
$_SESSION['values']['txtBthYear'] = '';
*/
$_SESSION['values']['txtEmail'] = '';
$_SESSION['values']['txtPhone'] = '';
$_SESSION['values']['txtCity'] = '';
$_SESSION['values']['selGender'] = '';
$_SESSION['values']['txtZip'] = '';
}
if (!isset($_SESSION['errors']))
{
if($_REQUEST[stand_custm]==2)
{
$_SESSION['errors']['selSize'] = 'hidden';
$_SESSION['errors']['selQuantity'] = 'hidden';
}
else if($_REQUEST[stand_custm]==1)
{
$_SESSION['errors']['txtHeight'] = 'hidden';
$_SESSION['errors']['txtWidth'] = 'hidden';
$_SESSION['errors']['txtDepth'] = 'hidden';
$_SESSION['errors']['txtQuantity'] = 'hidden';
}
$_SESSION['errors']['txtName'] = 'hidden';
$_SESSION['errors']['txtCompany'] = 'hidden';
/* $_SESSION['errors']['selGender'] = 'hidden';
$_SESSION['errors']['selBthMonth'] = 'hidden';
$_SESSION['errors']['txtBthDay'] = 'hidden';
$_SESSION['errors']['txtBthYear'] = 'hidden';
*/
$_SESSION['errors']['txtEmail'] = 'hidden';
$_SESSION['errors']['txtPhone'] = 'hidden';
$_SESSION['errors']['txtCity'] = 'hidden';
$_SESSION['errors']['selGender'] = 'hidden';
$_SESSION['errors']['txtZip'] = 'hidden';
}
?>
The attachment preview is chopped off after the first 10 KB. Please download the entire file.
<?php
// load error handler and database configuration
// Class supports AJAX and PHP web form validation
class Validate
{
// stored database connection
// constructor opens database connection
// supports AJAX validation, verifies a single value
public function ValidateAJAX($inputValue, $fieldID)
{
// check which field is being validated and perform validation
echo $_REQUEST[stand_custm];
if($_REQUEST[stand_custm]==2)
{
switch($fieldID)
{
case 'selSize':
return $this->validateSize($inputValue);
break;
case 'selQuantity':
return $this->validateQuantity1($inputValue);
break;
}
}
else if($_REQUEST[stand_custm]==1)
{
switch($fieldID)
{
case 'txtHeight':
return $this->validateHeight($inputValue);
break;
case 'txtWidth':
return $this->validateWidth($inputValue);
break;
case 'txtDepth':
return $this->validateDepth($inputValue);
break;
case 'txtQuantity':
return $this->validateQuantity($inputValue);
break;
}
}
switch($fieldID)
{
/*case 'selSize':
return $this->validateSize($inputValue);
break;
case 'selQuantity':
return $this->validateQuantity1($inputValue);
break;
*/ //Check if zipcode is valid
/*case 'txtHeight':
return $this->validateHeight($inputValue);
break;
case 'txtWidth':
return $this->validateWidth($inputValue);
break;
case 'txtDepth':
return $this->validateDepth($inputValue);
break;
case 'txtQuantity':
return $this->validateQuantity($inputValue);
break;*/
// Check if the username is valid
case 'txtUsername':
return $this->validateUserName($inputValue);
break;
// Check if the name is valid
case 'txtName':
return $this->validateName($inputValue);
break;
// Check if the name is valid
case 'txtCompany':
return $this->validateCompany($inputValue);
break;
// Check if a gender was selected
/*case 'selGender':
return $this->validateGender($inputValue);
break;
*/
// Check if birth month is valid
/* case 'selBthMonth':
return $this->validateBirthMonth($inputValue);
break;*/
// Check if birth day is valid
/* case 'txtBthDay':
return $this->validateBirthDay($inputValue);
break;*/
// Check if birth year is valid
/* case 'txtBthYear':
return $this->validateBirthYear($inputValue);
break;*/
// Check if email is valid
case 'txtEmail':
return $this->validateEmail($inputValue);
break;
// Check if phone is valid
case 'txtPhone':
return $this->validatePhone($inputValue);
break;
// Check if the name is valid
case 'txtCity':
return $this->validateCity($inputValue);
break;
case 'selGender':
return $this->validateGender($inputValue);
break;
// Check if zipcode is valid
case 'txtZip':
return $this->validateZip($inputValue);
break;
// Check if "I have read the terms" checkbox has been checked
/* case 'chkReadTerms':
return $this->validateReadTerms($inputValue);
break;*/
}
}
// validates all form fields on form submit
public function ValidatePHP()
{
// error flag, becomes 1 when errors are found.
$errorsExist = 0;
// clears the errors session flag
if (isset($_SESSION['errors']))
unset($_SESSION['errors']);
if($_REQUEST[stand_custm]==2)
{
$_SESSION['errors']['selSize'] = 'hidden';
$_SESSION['errors']['selQuantity'] = 'hidden';
}
else if($_REQUEST[stand_custm]==1)
{
$_SESSION['errors']['txtHeight'] = 'hidden';
$_SESSION['errors']['txtWidth'] = 'hidden';
$_SESSION['errors']['txtDepth'] = 'hidden';
$_SESSION['errors']['txtQuantity'] = 'hidden';
}
// By default all fields are considered valid
/* $_SESSION['errors']['selSize'] = 'hidden';
$_SESSION['errors']['selQuantity'] = 'hidden';*/
/* $_SESSION['errors']['txtHeight'] = 'hidden';
$_SESSION['errors']['txtWidth'] = 'hidden';
$_SESSION['errors']['txtDepth'] = 'hidden';
$_SESSION['errors']['txtQuantity'] = 'hidden';*/
$_SESSION['errors']['txtName'] = 'hidden';
$_SESSION['errors']['txtCompany'] = 'hidden';
/* $_SESSION['errors']['selGender'] = 'hidden';*/
$_SESSION['errors']['txtEmail'] = 'hidden';
$_SESSION['errors']['txtPhone'] = 'hidden';
$_SESSION['errors']['txtCity'] = 'hidden';
$_SESSION['errors']['selGender'] = 'hidden';
$_SESSION['errors']['txtZip'] = 'hidden';
// Validate username
/* if (!$this->validateUserName($_POST['txtUsername']))
{
$_SESSION['errors']['txtUsername'] = 'error';
$errorsExist = 1;
}*/
if($_REQUEST[stand_custm]==2)
{
if (!$this->validateSize($_POST['selSize']))
{
$_SESSION['errors']['selSize'] = 'error';
$errorsExist = 1;
}
if (!$this->validateQuantity1($_POST['selQuantity']))
{
$_SESSION['errors']['selquantity'] = 'error';
$errorsExist = 1;
}
}
// Validate Height
else if($_REQUEST[stand_custm]==1)
{
if (!$this->validateHeight($_POST['txtHeight']))
{
$_SESSION['errors']['txtHeight'] = 'error';
$errorsExist = 1;
}
if (!$this->validateWidth($_POST['txtWidth']))
{
$_SESSION['errors']['txtWidth'] = 'error';
$errorsExist = 1;
}
if (!$this->validateDepth($_POST['txtDepth']))
{
$_SESSION['errors']['txtDepth'] = 'error';
$errorsExist = 1;
}
if (!$this->validateQuantity($_POST['txtQuantity']))
{
$_SESSION['errors']['txtQuantity'] = 'error';
$errorsExist = 1;
}
}
// Validate name
if (!$this->validateName($_POST['txtName']))
{
$_SESSION['errors']['txtName'] = 'error';
$errorsExist = 1;
}
// Validate Company name
if (!$this->validateCompany($_POST['txtCompany']))
{
$_SESSION['errors']['txtCompany'] = 'error';
$errorsExist = 1;
}
// Validate gender
/*if (!$this->validateGender($_POST['selGender']))
{
$_SESSION['errors']['selGender'] = 'error';
$errorsExist = 1;
}*/
// Validate birth month
/*if (!$this->validateBirthMonth($_POST['selBthMonth']))
{
$_SESSION['errors']['selBthMonth'] = 'error';
$errorsExist = 1;
}*/
// Validate birth day
/* if (!$this->validateBirthDay($_POST['txtBthDay']))
{
$_SESSION['errors']['txtBthDay'] = 'error';
$errorsExist = 1;
}*/
// Validate birth year and date
/* if (!$this->validateBirthYear($_POST['selBthMonth'] . '#' .
$_POST['txtBthDay'] . '#' .
$_POST['txtBthYear']))
{
$_SESSION['errors']['txtBthYear'] = 'error';
$errorsExist = 1;
}*/
// Validate email
if (!$this->validateEmail($_POST['txtEmail']))
{
$_SESSION['errors']['txtEmail'] = 'error';
$errorsExist = 1;
}
// Validate phone
if (!$this->validatePhone($_POST['txtPhone']))
{
$_SESSION['errors']['txtPhone'] = 'error';
$errorsExist = 1;
}
// Validate city name
if (!$this->validateCity($_POST['txtCity']))
{
$_SESSION['errors']['txtCity'] = 'error';
$errorsExist = 1;
}
if (!$this->validateGender($_POST['selGender']))
{
$_SESSION['errors']['selGender'] = 'error';
$errorsExist = 1;
}
// Validate zipcode
if (!$this->validateZip($_POST['txtZip']))
{
$_SESSION['errors']['txtZip'] = 'error';
$errorsExist = 1;
}
// Validate read terms
/* if (!isset($_POST['chkReadTerms']) ||
!$this->validateReadTerms($_POST['chkReadTerms']))
{
$_SESSION['errors']['chkReadTerms'] = 'error';
$_SESSION['values']['chkReadTerms'] = '';
$errorsExist = 1;
}*/
// If no errors are found, point to a successful validation page
if ($errorsExist == 0)
{
return 'allok.php';
}
else
{
// If errors are found, save current user input
foreach ($_POST as $key => $value)
{
$_SESSION['values'][$key] = $_POST[$key];
}
return 'index.php';
}
}
// validate user name (must be empty, and must not be already registered)
/*private function validateUserName($value)
{
// trim and escape input value
$value = $this->mMysqli->real_escape_string(trim($value));
// empty user name is not valid
if ($value == null)
return 0; // not valid
// check if the username exists in the database
}*/
private function validateQuantity1($value)
{
// user must have a gender
return ($value == '0') ? 0 : 1;
}
private function validateSize($value)
{
// user must have a gender
return ($value == '0') ? 0 : 1;
}
// validate height
private function validateHeight($value)
{
// valid phone format: ###-###-####
return (!eregi('^[0-9]{0}-*[0-9]{1}-*[0-9]{1}$', $value)) ? 0 : 1;
}
private function validateWidth($value)
{
// valid phone format: ###-###-####
return (!eregi('^[0-9]{0}-*[0-9]{1}-*[0-9]{1}$', $value)) ? 0 : 1;
}
private function validateDepth($value)
{
// valid phone format: ###-###-####
return (!eregi('^[0-9]{0}-*[0-9]{1}-*[0-9]{1}$', $value)) ? 0 : 1;
}
private function validateQuantity($value)
{
// valid phone format: ###-###-####
return (!eregi('^[0-9]{0}-*[0-9]{1}-*[0-9]{1}$', $va
// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
// holds the remote server address
var serverAddress = "validate.php";
// when set to true, display detailed error messages
var showErrors = true;
// initialize the validation requests cache
var cache = new Array();
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
// will store the reference to the XMLHttpRequest object
var xmlHttp;
// this should work for all browsers except IE6 and older
try
{
// try to create XMLHttpRequest object
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// assume IE6 or older
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
// try every id until one works
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
{
try
{
// try to create XMLHttpRequest object
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e) {} // ignore potential error
}
}
// return the created object or display an error message
if (!xmlHttp)
displayError("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
// function that displays an error message
function displayError($message)
{
// ignore errors if showErrors is false
if (showErrors)
{
// turn error displaying Off
showErrors = false;
// display error message
alert("Error encountered: \n" + $message);
// retry validation after 10 seconds
setTimeout("validate();", 10000);
}
}
// the function handles the validation for any form field
function validate(inputValue, fieldID)
{
// only continue if xmlHttp isn't void
if (xmlHttp)
{
// if we received non-null parameters, we add them to cache in the
// form of the query string to be sent to the server for validation
if (fieldID)
{
// encode values for safely adding them to an HTTP request query string
inputValue = encodeURIComponent(inputValue);
fieldID = encodeURIComponent(fieldID);
// add the values to the queue
cache.push("inputValue=" + inputValue + "&fieldID=" + fieldID);
}
// try to connect to the server
try
{
// continue only if the XMLHttpRequest object isn't busy
// and the cache is not empty
if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
&& cache.length > 0)
{
// get a new set of parameters from the cache
var cacheEntry = cache.shift();
// make a server request to validate the extracted data
xmlHttp.open("POST", serverAddress, true);
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = handleRequestStateChange;
xmlHttp.send(cacheEntry);
}
}
catch (e)
{
// display an error when failing to connect to the server
displayError(e.toString());
}
}
}
// function that handles the HTTP response
function handleRequestStateChange()
{
// when readyState is 4, we read the server response
if (xmlHttp.readyState == 4)
{
// continue only if HTTP status is "OK"
if (xmlHttp.status == 200)
{
try
{
// read the response from the server
readResponse();
}
catch(e)
{
// display error message
displayError(e.toString());
}
}
else
{
// display error message
displayError(xmlHttp.statusText);
}
}
}
// read server's response
function readResponse()
{
// retrieve the server's response
var response = xmlHttp.responseText;
// server error?
if (response.indexOf("ERRNO") >= 0
|| response.indexOf("error:") >= 0
|| response.length == 0)
throw(response.length == 0 ? "Server error." : response);
// get response in XML format (assume the response is valid XML)
responseXml = xmlHttp.responseXML;
// get the document element
xmlDoc = responseXml.documentElement;
result = xmlDoc.getElementsByTagName("result")[0].firstChild.data;
fieldID = xmlDoc.getElementsByTagName("fieldid")[0].firstChild.data;
// find the HTML element that displays the error
message = document.getElementById(fieldID + "Failed");
// show the error or hide the error
message.className = (result == "0") ? "error" : "hidden";
// call validate() again, in case there are values left in the cache
setTimeout("validate();", 500);
}
// sets focus on the first field of the form
/*function setFocus()
{
document.getElementById("txtName").focus();
}
*/
<?php
// start PHP session
session_start();
// load error handling script and validation class
require_once ('validate.class.php');
// Create new validator object
$validator = new Validate();
// read validation type (PHP or AJAX?)
$validationType = '';
if (isset($_GET['validationType']))
{
$validationType = $_GET['validationType'];
}
// AJAX validation or PHP validation?
if ($validationType == 'php')
{
// PHP validation is performed by the ValidatePHP method, which returns
// the page the visitor should be redirected to (which is allok.php if
// all the data is valid, or back to index.php if not)
header("Location:" . $validator->ValidatePHP());
}
else
{
// AJAX validation is performed by the ValidateAJAX method. The results
// are used to form an XML document that is sent back to the client
$response =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' .
'<response>' .
'<result>' .
$validator->ValidateAJAX($_POST['inputValue'], $_POST['fieldID']) .
'</result>' .
'<fieldid>' .
$_POST['fieldID'] .
'</fieldid>' .
'</response>';
// generate the response
if(ob_get_length()) ob_clean();
header('Content-Type: text/xml');
echo $response;
}
?>
Manuz
2
Junior Poster
Hey,
I really do not go through ur codes okey...
Explain a little more buddy..What is the use of your radio buttons?(purpose)
If u want to use only one condition then give same name for all the radio buttons.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.