Re: Ajax
| From: | Jim Giner | Date: | Thu, 21 Feb 2013 14:53:17 +0000 |
| Subject: | Re: Ajax | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-48693@lists.php.net to get a copy of this message | ||
On 2/20/2013 7:27 PM, Ethan Rosenberg, PhD wrote:
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:March on. But without me. How you make a living in the programming world is beyond me.$results = "<table border=4 cellpadding=5 cellspacing=55 rules=allframe=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><tdid='nums$k'>$nums[$k]</td></tr>";} $results = "<table border=4 cellpadding=5 cellspacing=55 rules=allframe=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 Ethan - you are unbelievable. I've talked to you about this multiple times and thought I had you on the easy path, but you never cease to amaze me with your inability to grasp the obvious.