By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,667 Members | 2,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,667 IT Pros & Developers. It's quick & easy.

Ajax+PHP form validation

ak1dnar
Expert 100+
P: 1,584
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]
Mar 22 '07 #1
Share this Question
Share on Google+
2 Replies


acoder
Expert Mod 15k+
P: 16,027
Parse the response, e.g. add a delimiter such as "::", then use that to split the response.

As for your loading message, just set it to empty when the readystate is 4.

Why not just do the checking client-side? This is simple form validation. You don't need to use Ajax for that.
Mar 22 '07 #2

ak1dnar
Expert 100+
P: 1,584
Yes it is Acoder, this is a simple form.
I need to learn this then i can implement it for more complex form validation.

could you please give me a sample for this, any link or simple file.to understand the concepts.
Mar 22 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.