Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: AJAX and PHP

  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question AJAX and PHP

    I'm learning ajax and up to this point, I've made web-pages that have 1 form made up of javascript/html that refers to a php page that only has code to process (i.e. update, insert into db) the form from the html page.

    I now would like to have 3 unique registration pages, coded in javascript/html, and still have only 1 php page that does the inserting and updating, rather than having 3 php pages to do the backend work.

    I thought of creating functions in the php page and correspondingly have the variable data (in the javascript code) enclosed in the appropriate function, so when I send the data only the intended php code is executed. Is that the way to go? Or is there another, better way?

    Insight and suggestions are much appreciated. Thanks

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    in your form have a hidden field, for the example let's call it "which". set that field to a unique value. on your processor page have it pull that variable, then set up a switch based on it. you could have tons and tons of things built into one processor page that way.

  • #3
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the response. In the process of adjusting my code, I seemingly altered something that now causes my code to no longer work... I've gone over the code and I can't find what's wrong with it... I've tested both the javascript/ajax side and the php side of the code. What seems to be happening is that the ajax component is not sending or the php side is not accepting the posted variables. Below are my ajax and php codes, can someone take a look and let me know what they think/know is wrong with it? Thanks.

    Code:
    <script language="javascript">
    function callback(ajaxResponse){
       alert(ajaxResponse);
    }
    
    function sendData(){
       
       var XMLHttpRequestObj = false;
       
       var interval = document.forms['basicRegister'].elements['joinType'].length;
       for (i=0; i<interval; i++){
          if(document.forms['basicRegister'].elements['joinType'][i].checked){
             var joinType = document.forms['basicRegister'].elements['joinType'][i].value;
          }
       }
       var firstName = document.forms['basicRegister'].elements['firstName'].value;
       var lastName = document.forms['basicRegister'].elements['lastName'].value;
       var email = document.forms['basicRegister'].elements['email'].value;
       var password = document.forms['basicRegister'].elements['password'].value;
       
       var params = "firstName=" + firstName + "&" + "lastName=" + lastName + "&" + "email=" + email + "&" + "password=" + password + "&" + "joinType=" + joinType;
       
       if (window.XMLHttpRequest){
          XMLHttpRequestObj = new XMLHttpRequest();
       } else if (window.ActiveXObject){
          XMLHttpRequestObj = new ActiveXObject("Microsoft.XMLHttp");
          }
          
       if (XMLHttpRequestObj){
          var url = "registration.php";
          XMLHttpRequestObj.open("POST", url, true);
          XMLHttpRequestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          XMLHttpRequestObj.setRequestHeader("Content-length", params.length);
          XMLHttpRequestObj.setRequestHeader("Connection", "close");
       
          XMLHttpRequestObj.onreadystatechange = function(){
             if (XMLHttpRequestObj.readystate == 4 && XMLHttpRequestObj.status == 200){
                callback(XMLHttpRequest.responseText);
             }   
          }
          XMLHttpRequestObj.send(params);
       }
       
    }
    PHP Code:
    <?php

    include ("objects.php");

    $dbConn = new dbConnection();
    $format = new stringFormat(); 

    $connected $dbConn->conn(); // connect to db
    $dbConn->select_db(); // select db

    for ($i=0$i<6$i++) // generate id
    {
        
    $Id .= rand(0,1) ? chr(rand(48,57)) : chr(rand(97,122));
    }

    $firstName $_POST['firstName'];
    $lastName $_POST['lastName'];
    $email $_POST['email'];
    $password $_POST['password'];
    $joinType $_POST['joinType'];

    $insertQuery sprintf("INSERT INTO registration (Id, firstName, LastName, joinType, email, password) VALUES(%s, %s, %s, %s, %s, %s)",  
    $format->formatValue($Id),
    $format->formatValue($firstName),
    $format->formatValue($lastName), 
    $format->formatValue($joinType),
    $format->formatValue($email),
    $format->formatValue($password));

    $result mysql_query($insertQuery$connected);
    ?>
    Note: I've used objects in the php code and they are working correctly.

  • #4
    Regular Coder
    Join Date
    Sep 2008
    Posts
    205
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Internet Explorer

    The problem with the code was two incorrect variables in the onreadystatechange function:

    1. I did not capitalize the "S" in .readyState
    2. I left out "Obj" to XMLHttpRequest - the Http request object.

    Code:
    XMLHttpRequestObj.onreadystatechange = function(){
    if (XMLHttpRequestObj.readyState == 4 && XMLHttpRequestObj.status == 200){
    callback(XMLHttpRequestObj.responseText);
    }   
    }
    I do have 2 follow-up questions however, if anyone has the answer to them, please let me know. Thanks.

    1. The alert message that I've setup only occurs every other entry, i.e. the first time I fill and submit the form, the alert occurs; second time through, alert doesn't occur; third time, the alert occurs. Why is that?

    2. Once the first form is successfully inserted into the database, I'd like to display another form. How can I confirm a successful insert and only then display another, new form?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •