Hi, please help me on this.
Here i am having a simple form that consist with name and email fields and both are required fields.
i am going to validate this inputs using validate.php.
but since i am since i am using responseText object i couldn't display the error message for corresponding fields.
That means if name is blank error should appers in <span id="output_name">
if email is blank error should appers in <span id="output_email">
and one more problem the loading message is not removing from the page.
here is my coding.
[HTML] <html>
<head>
<title>Sample Ajax</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
<!--
body,td,th {
font-family: verdana;
font-size: 11px;
}
.style1 {color: #FF0000}
-->
</style></head>
<script language="javascript">
window.onerror = function(msg, err_url, line) {alert('Unknown Error :) ' + line);}
function makeObject()
{
var x;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
{
x = new ActiveXObject("Microsoft.XMLHTTP");
}else
{
x = new XMLHttpRequest();
}
return x;
}
var request = makeObject();
function post_method()
{
var name = sample_form.name.value;
var email = sample_form.email.value;
request.open('post', 'validate.php');
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.onreadystatechange = output;
request.send('name='+name+'&email='+email);
}
function output()
{
if(request.readyState == 1)
{
document.getElementById('load_msg').innerHTML = 'Loading...';
}
if(request.readyState == 4)
{
var errors = request.responseText;
document.getElementById('output_name').innerHTML = errors;
}
}
</script>
<body>
<DIV id="load_msg"></DIV>
<form action="" method="post" name="sample_form" id="sample_form">
Name : <input type="text" name="name"><span id="output_name" class="style1">* </span><BR>
Email : <input type="text" name="email"><span id="output_email" class="style1">* </span><br>
<input type="button" name="Button" value="Submit" onClick="post_method()">
</form>
</body>
</html>[/HTML]
validate.php
[PHP] <?php
$name = $_POST['name'];
$email = $_POST['email'];
if (stripslashes($name) == "" )
{
echo 'Name Cannot be left Blank';
}
if (stripslashes($email) == "" )
{
echo 'Email Cannot be left Blank';
}
?>
[/PHP]