Re: Ajax

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I think you need to set it here.

<script type="text/javascript">
   $(document).ready(function(){
           $("#e1").dblclick(function(e) {
		 var id = $(this).attr('id'); ///This was missing
                 $.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>


Best,
Karl


On Feb 20, 2013, at 7:39 PM, Ethan Rosenberg, PhD wrote:

On 02/20/2013 08:30 PM, Karl DeSaulniers wrote:

Hi Ethan,
Quick question. I'm either looking right over it or I do not see how/where var id is set.
Where is this at?

Code Eg:
cnm = "name"+id;

Best,
Karl


On Feb 20, 2013, at 6: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:

$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


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Karl DeSaulniers
Design Drumm
http://designdrumm.com

==============
Karl -

I do not see how/where var id is set

$results .= "<tr><td id='name$i' >$names[$k] </td><td id='nums$k'> $nums[$k]</td></tr>" ^^^^^^^ ^^^^^^^^

Is this the problem?? Poor formatting of the table row?

Ethan

Karl DeSaulniers
Design Drumm
http://designdrumm.com


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux