FanLee FanLee - 4 months ago 8
Javascript Question

jquery how to post to php, synchronztion

I want post to php, when id EmpAgree1,and then it's post variable EmpAgree=1; when id EmpAgree2,and then it's post variable EmpAgree=2,etc. But it is just read the last value of i, why? And how to resolve it? thx

$(document).ready(function() {
var i;
for( i=1; i <= <?php echo sizeof($id) ?> ; i++){
$("#EmpAgree"+i).click(function(){
$.post("information_receiving.php",{
EmpAgree : i
});
document.location.href="information_receiving.php";
});
}

Answer

You will use the the last value of i which is (sizeof($id)) in this case. To resolve it, try to store the number in the #EmpAgree elements:

$(document).ready(function() {
var i;
for( i=1; i <= <?php echo sizeof($id) ?> ; i++){
 $("#EmpAgree"+i).data('nbr', i).click(function(){
     $.post("information_receiving.php",{
         EmpAgree : $(this).data('nbr')
});
   document.location.href="information_receiving.php";
});
}
Comments