1

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.

7
  • ITs just a string right? Commented Mar 13, 2013 at 9:01
  • $.post('/path /to/ my /controller', {value: <?php echo $val; ?>}) Commented Mar 13, 2013 at 9:02
  • @Samy yes its just a string thing. Commented Mar 13, 2013 at 9:03
  • @Pete I did that but i am told if multiple rows are there in the table of the page that would only be capturing the latest value of the $val. Commented Mar 13, 2013 at 9:04
  • are you just wanting the val of the submitted row? Commented Mar 13, 2013 at 9:08

7 Answers 7

6

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){ 

    }); 
Sign up to request clarification or add additional context in comments.

15 Comments

thanks would you like to elaborate why this is better than the usual <?php echo $val ?>, Please . Thanks
i have mentioned it in my answer... ..if your script is in seperate .js file say script.js... you won't be allowed to use <?php ?> there... :).. (though in your case it looks like you have script in the same file) first one will work even if your script is in seperate file.....
My php and Jquery are in the same page and earlier i also did the same <?php echo $val ?> . But the guy who reviewed my code is saying it would cause an error.
yes... not error .. but there is some bug.. since your $val is in foreach loop... each item the loop runs .. $val gets replace.. so at the end your $val will jus have the last course_id... creating an array and pushing $val to array in each is another way to have all your values in $val
and you have button in each row?? i mean user has to click each button in a row to send that particular val??
|
2

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/

6 Comments

this would be sending the unique values even if there is multiple rows on each button click.
this will send the unique value of each row as it sends the $val associated to the row - looking at your original foreach loop, you put the $val into each button
Yes Pete I want to associate the unique $val to each button in the row and would like to send those values by the $.post() inside a button click function.
The above code will do that if the button code above is placed within your original foreach code, you then place the button click function into your document ready function
sorry, try replacing the + with . (dots), been a while since I did php!
|
1

Write them out on the page as a javascript variable.

1 Comment

Sure it is. But I am counting you too as a friend.
1

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){ 

     }); 
    });

4 Comments

If your post call is in seperate file .You could store the values in a hidden value and call get teh value while you are calling $.post
if there are multiple rows in the table then I have been told (and that's what has got me confused) that $val would be containing the value of the last row.
Which value you need? You need all row details need to be send?
No each row of the table has a button and on click of the button I am sending the relevant data which is unique to a row. Different rows are having different data.
1

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.

Comments

1

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){ 

          }); 
});

Comments

1

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."'>";
        ...        

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.