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

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>
<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}

<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");
x = new XMLHttpRequest();
return x;

var request = makeObject();
function post_method()
var name =;
var email =;'post', 'validate.php');
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.onreadystatechange = output;
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;

<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()">



[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';

Mar 22 '07 #1
Share this Question
Share on Google+
2 Replies

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

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 understand the concepts.
Mar 22 '07 #3

Post your reply

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