Ajax

From: Date: Thu, 21 Feb 2013 00:27:05 +0000
Subject: Ajax
Groups: php.db 
Request: Send a blank email to php-db+get-48688@lists.php.net to get a copy of this message
Dear List - I think we have to revisit this. The object is to click on an element of a table [name field] and retrieve the customer number[number field] send th eresult as an Ajax call to a callback function as a json string, and get back the customer number as a PHP variable. Code snippits: Start: <div id="e1"> Table:
    $results = "<table border=4 cellpadding=5 cellspacing=55  rules=all frame=box style='table-layout:fixed;'>";
    $results .= "<tr><th>Name</th><th>Number</th></tr>";
    // generate a table here
    for($i=0; $i<10; $i++)
    {
        $k = $i ;
        $results .= "<tr><td id='name$i' >$names[$k] </td><td  id='nums$k'>$nums[$k]</td></tr>";
    }
    $results = "<table border=4 cellpadding=5 cellspacing=55  rules=all frame=box style='table-layout:fixed;'>";
    $results .= "<tr><th>Name</th><th>Number</th></tr>";
    }
    $results .= "</table>";
    $results;
retrieve number: <script type="text/javascript">
    var cusn;
    $(document).ready(function(){
        $("#e1").click(function() {
            cusn = $(this).attr('id');
            alert(cusn); // this always returns e1
        });
    });
</script> Ajax Call <script type="text/javascript">
    $(document).ready(function(){
            $("#e1").dblclick(function(e) {
                 $.post("testClickcallback.php",
                   {"custnum":cusn},  function getCustNo(id)
        {
            cno = 'cust'+id;
            cnm = "name"+id;
            cname = document.getElementById(cnm).innerHTML;
            cnum = document.getElementById(cno).innerHTML;
        alert("For customer "+cname+" the number is "+cnum);
            return;
        }
//    alert("subval");
//alert(subval); ,"html"); }); }); </script> // receive error: document.getElementById(...) is null Callback function: <?php $json = Array(); $json['custnum'] = $_POST['cusn']; echo json_encode($json); ?> //response: | ||Notice: Undefined index: cusn in /var/www/testClickcallback.php on line 5 || ||Call Stack:
||     0.0005     126856   1. {main}() /var/www/testClickcallback.php:0
|| || ||Variables in local scope (#1): || $json = array () || ||{"custnum":null} Now what??||||| Ethan

« previous php.db (#48688) next »