php form validation using ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghjk
    Contributor
    • Jan 2008
    • 250

    php form validation using ajax

    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
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    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

    Comment

    • prabirchoudhury
      New Member
      • May 2009
      • 162

      #3
      you try some thing first and then ask for help.. better to have more specific.. lots of tutorial and example online ajax form validation

      Comment

      • ghjk
        Contributor
        • Jan 2008
        • 250

        #4
        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
        ======
        [code=php]
        <?php

        $array = array('Name'=>' FullName','Gend er'=>'Gender',' PhnNo' =>'PhoneNumber' ,'Email' =>'EmailAddress ','Address' =>'Address');

        ?>

        <form action="test.ph p" method="post" id="test" >
        .....
        <tr><td height="24" >Full Name</td>
        <td colspan="2"><in put type="text" name="Name" size="40"/></td>
        </tr>
        <tr>
        <td>Gender</td>
        <td width="69"><inp ut name="Gender" type="radio" value="1" /> Male</td>

        <td width="297" ><input name="Gender" type="radio" value="2" />Female&nbsp; </td>
        </tr>

        <tr>
        <td>Phone Number</td>
        <td colspan="2"><in put type="text" name="PhoneNumb er" /></td>
        </tr>

        <tr>
        <td>Email Address</td>
        <td colspan="2"><in put type="text" name="Email" /></td>
        </tr>
        <tr>
        <td>Address</td>
        <td colspan="2"><in put type="text" name="Address" size="40" /></td>
        </tr>

        <tr>

        <td colspan="2" align="right">
        <input name="Submit" value="Submit" id="Submit" src="../images/Submit.jpg" type="image" alt="add" height="30" width="73" onclick="add(); " />
        </td>
        </tr>
        </form>
        .....
        [/code]

        test.js
        ======
        Code:
        function ajax() {
        	var request;
            try {
                request=new XMLHttpRequest();
            }
            catch (e) {
                try {
                    request=new ActiveXObject("Msxml2.XMLHTTP");
                }
                  catch (e) {
                    try {
                        request=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e) {
                        alert("Your browser does not support AJAX!");
                        return false;
                    }
                }
            } 
        	return request;
        }
        	
        
        function add(Name,Gender,PhnNo,Email,Address){
        //alert ('test')
        if (PhnNo){
        	PhnNo= document.getElementById(PhnNo).value
        	var regex = /^\d{11}$/;
        	if( !PhnNo.match(regex) ) 
         		return "Number is incorrect!!";
        	}
        
        }
        Last edited by acoder; Jun 25 '09, 01:52 PM. Reason: Changed [quote] to [code] tags

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          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.

          Comment

          Working...