PHP problem with include (containing AJAX) function

Discussion in 'PHP' started by admins, Mar 8, 2011.

  1. #1
    I have written few pages but facing problem. Pages are running individually ok, but from home page is not working.

    Total 7 pages are used: 5 .php and 2 .js

    My pages are
    1. customer.php (home page)
    2. getcustomer.php
    3. getuser.php
    4. new_customer.php
    5. old_customer.php
    and
    6. selectuser.js
    7. selectcustomer.js

    5 php pages are attached.

    2 .js files are below:
    ## //selectuser.js
    
    var xmlHttp;
    function showUser(str)
    { 
        xmlHttp=GetXmlHttpObject();
        if (xmlHttp==null)
         {
             alert ("Browser does not support HTTP Request");
             return;
         }
        var url="getuser.php";
        url=url+"?q="+str;
        url=url+"&sid="+Math.random();
        xmlHttp.onreadystatechange=stateChanged;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }
    function stateChanged() 
    { 
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
        {
            document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
        }
    }
    function GetXmlHttpObject()
    {
        var xmlHttp=null;
        try
        {
            // Firefox, Opera 8.0+, Safari
            xmlHttp=new XMLHttpRequest();
        }
        catch (e)
        {
             //Internet Explorer
             try
             {
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
             }
             catch (e)
             {
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
             }
        }
        return xmlHttp;
    }
    
    Code (markup):


    ## //selectcustomer.js

    
    var xmlHttp;
    function showCustomer(str)
    { 
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
     {
     alert ("Browser does not support HTTP Request");
     return;
     }
    var url="getcustomer.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged; 
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    }
    function stateChanged() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
     document.getElementById("txtCustomer").innerHTML=xmlHttp.responseText;
     } 
    }
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
     {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     //Internet Explorer
     try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     }
    return xmlHttp;
    }
    
    Code (markup):
    -----------------------------------------------
    Problem: When I click old customer from customer.php, then it is showing Customer No and when I put 1 or 2 it is showing nothing.

    But, if I run getuser.php like getuser.php?q=1
    then, it is showing correctly.

    I have found nothing wrong, because from getuser.php code is OK.

    Problem is, from customer.php customer no is not working. Can I get any suggestion?
     

    Attached Files:

    admins, Mar 8, 2011 IP
  2. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The values of the radio buttons are 0 (new customer) and 1 (old customer) but getcustomer.php is testing for values 1 and 2 while getuser.php tests for 0 and 1 ...
     
    mallorcahp, Mar 8, 2011 IP
  3. admins

    admins Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No, this is not problem.
    From customer.php : 0 for new_customer.php and 1 for old_customer.php
    and
    old_customer.php is calling the page getcustomer.php which contains 1 and 2 so that getcustomer returns 1/2 to old_customer.php

    If I run old_customer.php or from getuser.php then code is running.
    But, if I call old_customer.php from customer.php then old_customer.php can't execute code from getcustomer.php
     
    admins, Mar 9, 2011 IP
  4. mallorcahp

    mallorcahp Peon

    Messages:
    141
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    OK, see what you're trying to do now ...

    The javascript in the ajax response is not being evaluated, basically the response is interpreted as a string therefore showCustomer is never called ...

    Move <script src="selectcustomer.js"></script> to customer.php

    Edit selectcustomer.js
    rename stateChanged() function to CustomerStateChanged() to avoid conflict
    change showCustomer() function so that onreadystatechange calls renamed function : xmlHttp.onreadystatechange=CustomerStateChanged;
    delete the GetXmlHttpObject() function from selectcustomer.js to avoid being conflict

    You could move all the js to a single file and maybe look at a framework such as prototype or jquery which provide better ajax handlers
     
    mallorcahp, Mar 9, 2011 IP
  5. admins

    admins Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you. Problem solved according to change you mention.
    I have also merged .js files into a single file and calling from customer.php only. Code is quite OK.
    I have donwloaded protype as well as jquery, but it seems to me that they need time to adopt. So far thanks. Rep added.
     
    admins, Mar 9, 2011 IP