问题描述:

In the following Php code I would like to access the values stored in my $val variable from my jQuery script so as to be able to send a ajax call.

For each table row the $val would be containing unique values, I need to access the unique

values to be able to send to my post request.

 <?php

if($batch_query != null){

$i = 0;

foreach($batch_query->result_array() as $row){

$val = "'".$row['course_id'].",".$row['center_id'].",".$row['batch_id']."'";//These values are coming from the server side and I need to send it to the controller for the Ajax.

echo "<tr>";

echo "<td>".$row['course_name']."</td>";

echo "<td>".$row['center_name']."</td>";

echo "<td>".$row['batch_name']."</td>";

echo "<td>"."<button id= \"btnnumber_$i\" class='btn info toggler' value=$val>Show Info <i class='icon-arrow-down'></i></button>"."</td>";// here I am using the value attribute and assigning it the value $val.

}

}

?>

Here is my JQuery

 $(function(){

$.post('/path /to/ my /controller', {value: $val}).done(function(data){

});

});

How to get those values in the $val ?

Any help will be highly appreciated.

网友答案:

I would change the button so it puts the $val into a data attribute like so:

"<button id=\"btnnumber_" + $i +"\"  class=\"btn info toggler\" data-value=\"" + $val + "\">Show Info  <i class=\"icon-arrow-down\"></i></button>"

then I would change your jquery to something like

$('button').click(function() {
  $.post('/path /to/ my /controller', 
     {value: $(this).data('value')}).done(function(data){ 

  }); 
});

Here is a fiddle example of the button click getting the data-value attribute: http://jsfiddle.net/T7cJR/

网友答案:

the better way in jquery is..

$(function(){ 
  var btnvalue=$('button.info').attr('value');
   $.post('/path /to/ my /controller', {value: btnvalue}).done(function(data){ 

    }); 
});

and simplest (which is not possible if you have seperate .js file)...

var btnvalue= <?php echo $val ?>
 $.post('/path /to/ my /controller', {value: btnvalue}).done(function(data){ 

    }); 
网友答案:

Write them out on the page as a javascript variable.

网友答案:

If your jquery is in the same page you can use the below.

    $(function(){
        $.post('/path /to/ my /controller', 
         {value: <?php echo   $val?>}).done(function(data){ 

     }); 
    });
网友答案:

Use it like this :<?php echo $val; ?>

$.post('/path /to/ my /controller', {value: <?php echo $val; ?>}).done(function(data){

You need to assign value to this variable before this line.

网友答案:

Write the code to get all values in the $var variable. Then at the end of page inside script tag, write this

$(function(){
          $.post('/path /to/ my /controller', {value: <?php echo $val ?>}).done(function(data){ 

          }); 
});
网友答案:

you can save the data as tr attribute then access it with jquery:

<?php 
 if($batch_query != null){
    $i = 0;  
    foreach($batch_query->result_array() as $row){
       $val = "'".$row['course_id'].",".$row['center_id'].",".$row['batch_id']."'";//These values are coming from the server side and I need to send it to the controller for the Ajax. 
       echo "<tr id='".$val."'>";
        ...        
相关阅读:
Top