Connecting Tech Pros Worldwide Forums | Help | Site Map

php form validation using ajax

Familiar Sight
 
Join Date: Jan 2008
Posts: 203
#1: Jun 24 '09
I' developing web site with php and postgres. I want to validate php forms using ajax. I'm new to ajax and tried to several examples. But still I couldn't get it. Could someone please tell me a complete example in php and ajax?

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,246
#2: Jun 24 '09

re: php form validation using ajax


have a look here for an example where AJAX and php work together ... simply post back in case you have specific problems with your code.

kind regards
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 159
#3: Jun 25 '09

re: php form validation using ajax


you try some thing first and then ask for help.. better to have more specific.. lots of tutorial and example online ajax form validation
Familiar Sight
 
Join Date: Jan 2008
Posts: 203
#4: Jun 25 '09

re: php form validation using ajax


Thanx for everyone. Now I'm little bit ok with ajax. These are my php form and ajax file. I want to check array values in php file using ajax function. I tried just one variable and it didn't work. Could you please tell me when I went wrong?
test.php
======
Expand|Select|Wrap|Line Numbers
  1.     <?php 
  2.  
  3. $array = array('Name'=>'FullName','Gender'=>'Gender','PhnNo' =>'PhoneNumber','Email' =>'EmailAddress','Address' =>'Address');
  4.  
  5.     ?>
  6.  
  7.     <form action="test.php" method="post" id="test"  >
  8.                  .....
  9.     <tr><td height="24" >Full Name</td>
  10.         <td colspan="2"><input type="text" name="Name"  size="40"/></td>
  11.     </tr>
  12.     <tr>
  13.         <td>Gender</td>
  14.         <td width="69"><input name="Gender" type="radio" value="1" /> Male</td>
  15.  
  16.         <td width="297"  ><input name="Gender" type="radio" value="2" />Female&nbsp;</td>
  17.     </tr>
  18.  
  19.     <tr>
  20.         <td>Phone Number</td>
  21.         <td colspan="2"><input type="text" name="PhoneNumber" /></td>
  22.     </tr>
  23.  
  24.     <tr>
  25.         <td>Email Address</td>
  26.         <td colspan="2"><input type="text" name="Email" /></td>
  27.     </tr>
  28.     <tr>
  29.         <td>Address</td>
  30.         <td colspan="2"><input type="text" name="Address" size="40" /></td>
  31.     </tr>
  32.  
  33.     <tr>
  34.  
  35.         <td colspan="2" align="right">
  36.         <input name="Submit" value="Submit" id="Submit" src="../images/Submit.jpg" type="image"  alt="add" height="30"  width="73"   onclick="add();" />
  37.         </td>
  38.     </tr>
  39.     </form>
  40.     .....
  41.  
test.js
======
Expand|Select|Wrap|Line Numbers
  1. function ajax() {
  2.     var request;
  3.     try {
  4.         request=new XMLHttpRequest();
  5.     }
  6.     catch (e) {
  7.         try {
  8.             request=new ActiveXObject("Msxml2.XMLHTTP");
  9.         }
  10.           catch (e) {
  11.             try {
  12.                 request=new ActiveXObject("Microsoft.XMLHTTP");
  13.             }
  14.             catch (e) {
  15.                 alert("Your browser does not support AJAX!");
  16.                 return false;
  17.             }
  18.         }
  19.     } 
  20.     return request;
  21. }
  22.  
  23.  
  24. function add(Name,Gender,PhnNo,Email,Address){
  25. //alert ('test')
  26. if (PhnNo){
  27.     PhnNo= document.getElementById(PhnNo).value
  28.     var regex = /^\d{11}$/;
  29.     if( !PhnNo.match(regex) ) 
  30.          return "Number is incorrect!!";
  31.     }
  32.  
  33. }
  34.  
  35.  
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,750
#5: Jun 25 '09

re: php form validation using ajax


Your call to the add() function doesn't have any parameters, so the function won't do anything. There's no reference to the Ajax function. If you want to use PHP code to validate, then there's no point in using JavaScript validation as well. Make an Ajax request to your PHP validation script instead.
Reply