hi, i want to use ajax to validate a form. here is my work including 2 files, main.html and process.php
main.html
[html]
<script language="javascript">
//initialize xmlhttprequest object...
var request=false;
try
{
request=new XMLHttpRequest();//for FF & IE 7
}
catch(e)
{
try
{
request=new ActiveXObject('Msxml2.XMLHTTP'); //for IE5+
}
catch(e)
{
try
{
request=new ActiveXObject('Microsoft.XMLHTTP');//for IE5
}
catch(e)
{
request=false;
alert('AJAX not supported');
}
}
}
function execute()
{
if(request.readyState==4 && request.status==200)
{
document.getElementById('errorname').innerHTML=req uest.responseText;
}
}
function validate(value,id)
{
srv="process.php?"+value
request.open('GET',srv)
request.onreadystatechange= function()
{
if(request.readyState==4 && request.status==200)
{
document.getElementById(id).innerHTML=request.resp onseText;
}
}
request.send(null)
}
</script>
<table width="500">
<tr>
<td>Username:</td>
<td><input type="text" name="username" autocomplete="off" onchange="validate('name='+this.value,'errorname') " /></td>
</tr>
<tr><td colspan="2"><div id="errorname"></div></td></tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email" autocomplete="off" onchange="validate('email='+this.value,'erroremail ')" /></td>
</tr>
<tr><td colspan="2"><div id="erroremail"></div></td></tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" autocomplete="off" onchange="validate('phone='+this.value,'errorphone ')" /></td>
</tr>
<tr><td colspan="2"><div id="errorphone"></div></td></tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" autocomplete="off" onchange="validate('address='+this.value,'erroradd ress')" /></td>
</tr>
<tr><td colspan="2"><div id="erroraddress"></div></td></tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" onclick="validate('submit=ok','errorsubmit')" value="Submit" /></td>
</tr>
<tr><td colspan="2"><div id="errorsubmit"></div></td></tr>
</table>
</body>
</html>
[/html]
process.php
[php]
<?php
include('db.php');
function valid_name($str)
{
return ( ! preg_match("/^([-a-z0-9])+$/i", $str)) ? FALSE : TRUE;
}
function valid_phone($str)
{
return ( ! preg_match("/^([0-9])+$/i", $str)) ? FALSE : TRUE;
}
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if($_GET['name'])
{
$name=$_GET['name'];
$q1=mysql_query("SELECT username FROM users WHERE username = '$name'") or die(mysql_error());
if(valid_name($name)==FALSE) die('<font color=red>Invalid username</font>');
if($name==''|| strlen($name)<3) die('<font color=red>Username must be at least 3 characters</font>');
if(mysql_num_rows($q1)!=0) die ('<font color=red>Name is in use');
else
{
echo $nameok=1;
echo '<font color=blue>Name OK!</font>';
}
}
if($_GET['email'])
{
$email=$_GET['email'];
if(($email=='')||(valid_email($email)==FALSE)) die("<font color=red>Invalid Email Address!</font>");
else
{
echo '<font color=blue>Email OK</font>';
echo $emailok=1;
}
}
if($_GET['phone'])
{
$phone=$_GET['phone'];
if(($phone=='')|| (valid_phone($phone)==FALSE)) die("<font color=red>Invalid phone number!</font>");
else
{
echo '<font color=blue>Phone OK</font>';
echo $phoneok=1;
}
}
if($_GET['address'])
{
$address=$_GET['address'];
if($address=='') die("<font color=red>Address has not been filled in!</font>");
else
{
echo '<font color=blue>Address OK!</font>';
echo $addressok=1;
}
}
if($_GET['submit'])
{
echo $emailok;
echo $addressok;
echo $phoneok;
echo $nameok;
if(($nameok==1)&&($addressok==1)&&($phoneok==1)&&( $emailok==1))
{
echo 'you are done!';
}
die('Ouch!');
}
?>
[/php]
each time user input satisfies the given condition I mark it with value 1 ($nameok=1, for example). at the very final step of the work when submitting, however, all the marks assigned seem to get back to 0 (as function echo gives me nothing)
anyone can show me what's wrong here?