SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ajax call works in one program, and not the other

    Here are three files, testclick7.php [the whole file] and a code snippit from Purchases4.php [testor.php] and the callback function tor testclick7 [testClickcallback.php] and Purchases4. I am trying to duplicate in Purchases4 what I do in testclick7; ie, to click on a customer name and retrieve the numeric customer id.

    The AJAX call works in testclick7 but fails in Purchases4.

    The error message I receive is on the following line:

    echo "<td><span id='name$i' onclick='getCustNo(name$i)'>$ln </span></td>";

    error: reference error: 'getCustNo() is not defined.


    testclick7.php:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <html>
        <head>
                <link type="text/css" href="jquery-ui-1.8.9.custom.css" rel="stylesheet" />
                <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
                <script type="text/javascript" src="jquery-ui-1.8.10.custom.min.js"></script> 
        </head>        
            
        <div id="e1">

    <?php
    error_reporting
    (0);
        
    // make up some data in arrays here
        
    $names = array("Jim","Joe","Stu","Ethan","Adam","Linda","Mary","Alice","Ann","Jeanne");
        
    $nums = array(1,5,4,99,23,45,64,84,91,25);
        global 
    $cnum;
        echo 
    "<table border=4 cellpadding=5 cellspacing=55  rules=all frame=box style='table-layout:fixed;  border-collapse:collapse;'>";
        echo 
    "<tr><th>Name</th><th>Number</th></tr>";
        
    // generate a table here
        
    for($i=0$i<10$i++)
        {
            
    $k $i ;
            echo 
    "<tr>
                    <td><span id='name
    $i' onclick='getCustNo($k)'>$names[$k]</span></td>
                    <td id='cust
    $k'>$nums[$k]</td>
                </tr>"
    ;
            
        }
        echo 
    "</table>";
        
        
    DisplayPage();
        
    MakeCustIndx();
        
    ?>
        </div>    

        
            <script type="text/javascript">
            function getCustNo(id)
            {
                cno = 'cust'+id;
                cnm = "name"+id;
                cname = document.getElementById(cnm).innerHTML;
                cnum = document.getElementById(cno).innerHTML;
        $(document).ready(function(){
                $("#m2").dblclick(function(e) { 
                     $.post("testClickcallback.php",
                       {"custnum":cnum}, addtext(), "html");
                });
             });
            }
        </script>


    <?php    
        
    function MakeCustIndx()
        {
            global 
    $cnum,$cno;
    ?>
                <link type="text/css" href="jquery-ui-1.8.9.custom.css" rel="stylesheet" />
                <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
                <script type="text/javascript" src="jquery-ui-1.8.10.custom.min.js"></script>  


      <script type="text/javascript"> 
        $(document).ready(function(){
                $("#e1").dblclick(function(e) { 
                     $.post("testClickcallback.php",
                       {"custnum":cnum}, addtext(), "html");
                });
     });

    </script> 
        


    <?php
        
    }
    ?>
    <script type="text/javascript">

        function addtext() {
            var newtext1 = cnum;
            document.poster.cnum.value   = newtext1;
    }
    </script>
        
    <div id="frm">

        <form   name="poster"  method="post"  action="">
          <input type="text" name="cnum" hidden="hidden" ></input>     
         <center><input type="submit" value="Validate Choice" /></center>
        </form>

    </div>    
    <?php        

        
        
    if( isset($_POST['cnum']))
        {
            echo 
    "post<br>";
            
    print_r($_POST);
            
    var_dump($_POST);
            
    $cust_num $_POST['cnum'];
            echo 
    "post<br>";
            echo 
    "$cust_num <br />";
            
    define("CUN",     "$cust_num");
            echo 
    CUN;
            
        }
        
        exit();
    //******************************************
    function DisplayPage()
    {
        global 
    $results;
        
    $code=<<<heredocs
        <html>
            <head>
            <title>Get Customer Number</title>

            </head>
                <body>
                    
    $results
                </body>
    heredocs;
        echo 
    $code;
    ?>
            </html>
    <?php    
    }
    testor.php:

    PHP Code:
        
                    <div id="a1">
    <?php                
                    
    if(count($errors_array) == 0)
                    {
                        
    ?>                    
        
                        
                        <center><b>Search Results</b></center><br />
                            <center>
                            <table border=4 cellpadding=5 cellspacing=55  rules=all frame=box style='table-layout:fixed;  border-collapse:collapse;'>
                            <tr class=heading>
                            <th>Last Name</th>
                            <th>First Name</th>
                            <th>Street</th>
                            <th>City</th>
                            <th>State</th>
                            <th>Zip</th>
                            <th>Phone</th>
                            <th>Notes</th>
                            <th>Cust_Num</th>
                            
                            </tr>
    <?php                        
                            
                    $i 
    0
                        do 
                        {
                            {

                                
                                
    $vara2 = array(array($Cust_Num$Fname$Lname$Street$City$State$Zip$Phone$Notes));
                                
    $vara2[$i][0]    = $Cust_Num;                
                                
    $vara2[$i][1]    = $Fname;                    
                                
    $vara2[$i][2]    = $Lname;    
                                
    $vara2[$i][3]    = $Street;                    
                                
    $vara2[$i][4]    = $City;                
                                
    $vara2[$i][5]    = $State;                    
                                
    $vara2[$i][6]    = $Zip;                
                                
    $vara2[$i][7]    = $Phone;                    
                                
    $vara2[$i][8]    = $Notes;        
                                
                                    
                        
    $nm $vara2[$i][0];
                        
    $ln $vara2[$i][];
                        echo 
    "<tr>";
                            echo 
    "<td><span id='name$i' onclick='getCustNo(name$i)'>$ln </span></td>";
                            echo 
    "<td id='cust$i'>$nm</td>";
                            echo 
    "</tr>";
                                
        
                            } while (
    mysqli_stmt_fetch($stmt)); //end do-while
                            
    $imax $i;
                            echo 
    "</table>";
                            echo 
    "</center>";
                            
    DisplayPage();
                            
    MakeCustIndx();

                            }    
    //    end count($errors_array)
    ?>                        
                    </div>
            <script type="text/javascript">
            function getCustNo(id)
            {
                cno = 'cust'+id;
                cnm = "name"+id;
                cname = document.getElementById(cnm).innerHTML;
                cnum = document.getElementById(cno).innerHTML;
        $(document).ready(function(){
                $("#m2").dblclick(function(e) { 
                     $.post("testClickcallback.php",
                       {"custnum":cnum}, addtext(), "html");
                });
             });
            }
        </script>

    <?php
        
    function MakeCustIndx()
        {
            global 
    $cnum,$cno;
    ?>
                <link type="text/css" href="jquery-ui-1.8.9.custom.css" rel="stylesheet" />
                <script type="text/javascript" src="jquery-1.6.2.min.js"></script>
                <script type="text/javascript" src="jquery-ui-1.8.10.custom.min.js"></script>  

      <script type="text/javascript"> 
        $(document).ready(function(){
                $("#e1").dblclick(function(e) { 
                     $.post("testClickcallback.php",
                       {"custnum":cnum}, addtext(), "html");
                });
             });

    </script> 
        


    <?php
        
    }
    ?>
    <script type="text/javascript">

    function addtext() {
        var newtext1 = cnum;
        document.poster.cnum.value   = newtext1;
    }
    </script>
        
    <div id="frm">

    <form   name="poster"  method="post"  action="">
      <input type="text" name="cnum" hidden="hidden" ></input>     
     <center><input type="submit" value="Validate Choice" /></center>
    </form>

    </div>    
    <?php        

        
        
    if( isset($_POST['cnum']))
        {
            echo 
    "post<br>";
            
    print_r($_POST);
            
    var_dump($_POST);
            
    $cust_num $_POST['cnum'];
            echo 
    "post<br>";
            echo 
    "$cust_num <br />";
            
    define("CUN",     "$cust_num");
            echo 
    CUN;
            
        }
        
        exit();
    //******************************************
            
    function DisplayPage()
            {
                global 
    $results;
                
    $code=<<<heredocs
                <html>
                    <head>
                    <title>Get Customer Number</title>

                    </head>
                        <body>
                
    $results
                        </body>
    heredocs;
                echo 
    $code;
            
    ?>
                    </html>
            <?php    
            
    // end DisplayPage
        
    }// end handle data
    testClickcallback.php:

    PHP Code:
    <?php  
    $json 
    = Array();
    $json['cnum'] =  $_POST['custnum'];
    echo 
    json_encode($json); 
    ?>
    Thanks.

    Ethan

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    One thing that caught my eye in testor.php is
    PHP Code:
    do 
                        {
                            { 
    Two opening brackets after the do ?

    And since the error is a javascript error, you might want to check out the code that is actually sent to your browser.

  3. #3
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    The structure is:

    $i = 0;
    do
    {
    {
    <tr>
    .
    .
    .
    </tr>
    }
    $i++;
    }while(...);
    </table>

    Without the second } only one line of data is displayed.
    I hope this clarifies the issue.

  4. #4
    SitePoint Member
    Join Date
    Sep 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One other point: I am using Firebug. The Ajax call never fires. The only error I get is: error: reference error: 'getCustNo() is not defined.


Bookmarks

Posting Permissions

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