473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Ajax+PHP form validation

ak1dnar
1,584 Expert 1GB
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
2 3052
acoder
16,027 Expert Mod 8TB
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
1,584 Expert 1GB
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

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

Similar topics

2
by: Igor | last post by:
Hi I'm writing script using AJAX and PHP to upload files. I'm trying to create progress bar to show how many bytes have been already uploaded to server. Can I get the total file size (using...
7
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I...
1
by: pittendrigh | last post by:
Are there any open source ajax/php html editors? I want to incorporate as simple (very simple) ajax based html editor into a webapp I'm making. I spent an hour or so downloading code and saw...
4
by: moho | last post by:
hi, I'm wondering how to get the fields from a list that I have. When sending it to the PHP page it doesn't seem to see it. I'm using this script for sending it to the PHP page. The getformvalues...
1
ak1dnar
by: ak1dnar | last post by:
Hi i need to download some files (.pdf/.doc) from web root. I am using a ajax script to send the file IDs and PHP will process it and it should generate the save as dialog box. Currently without...
3
Haitashi
by: Haitashi | last post by:
I have a multi-page form. The first page asks for an email and password. I want the user to enter their email address & password twice before they can click submit. The post action on this page is...
6
by: John Doe | last post by:
Here's my issue: I have an instant messenger type feature for my site, its basically an ajax IM feature. I run with a mysql backend on the site, i have a div on my main page that runs a javascript...
0
by: aua_usama | last post by:
Dear all, i am using ajax framework ver 1.0, it is working fine with english when i switch the culture to arabic the validation message is totally corrupted, the proplem occure with all RTL...
1
by: Amzul | last post by:
hello all i post the same Q in php forum i believe here its more suitable. was wondering if anyone have a snipt of accordion in ajax/php before i have to dig in the .net section does it have...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.