0

I have script like this

 function getval(sel) {
         var id= sel.value;  
                $.ajax({
                        type:"POST",
                        url:"./tab.php",
                        data:{id:id,task:'search'},
                         success: function(response){
                             //(I don't know what i should write for pass to php code)
                         }
            });
    }

I don't know how I can pass data response to php code ? For Example: if I alert response, it's show 123 .so I want pass value 123 to a variable in php $id = 123

start
  • 1
  • 5

3 Answers3

0

response is the result passed BY php, not TO php. What's passed to php is the id and the task.

In your tab.php, you can access these two values :

<?php
$id = $_POST['id'];
$task = $_POST['task'];
//do anything you want with it...
?>
Veve
  • 6,643
  • 5
  • 39
  • 58
0

This is not the right workflow. PHP executes on runtime, so every time the page has finished loading you can't really put variables back into PHP (unless you reload the page). This is where AJAX comes in, so you can call PHP scripts from JavaScript / jQuery and don't have to reload the page every time.

The right thing to do is to either store the variable you have generated in the tab.php script in a database, $_SESSION, $_COOKIE or something similar like so:

//put this at the top of all your php scripts you want to use the variable in
session_start();
//now store the variable you wanted to pass (in tab.php)
$_SESSION['returnValue'] = $myValue;

Now if you want to use the variable in other pages just echo it like so (remember to add session_start() at the top of the PHP script.

echo $_SESSION['returnValue'];
Erik Terwan
  • 2,710
  • 19
  • 28
0

First of all, start by reading this

To answer your question,

function getval(sel) {
   var id= sel.value;  
   $.ajax({
      type:"POST",
      url:"./tab.php",
      data:{id:id,task:'search'},
      success: function(response){
         //(I don't know what i should write for pass to php code)
      }
    });
}

The result from id and task are sent via $_POST (type:"POST") to the page tab.php (url:"./tab.php"). If you want to do that on a different page, just change the url in your ajax call: url:"./any_other_page.php" and the posted values will be sent there

Finally, read THIS post as well. It is greatly written and very well explained.

Hope it helps!
Keep on coding!
Ares.

Community
  • 1
  • 1
Ares Draguna
  • 1,641
  • 2
  • 16
  • 32
  • `id` that I pass to `tab.php` is a variable for pass to get `id` to query to get data in this page – start Oct 22 '14 at 11:01
  • I understood 50% of what you just said... you have to pass `id` with $_GET to a different page? – Ares Draguna Oct 22 '14 at 11:02
  • I have ask it again with it http://stackoverflow.com/questions/26513808/php-how-to-used-select-box-for-search-data-in-list-view-table – start Oct 22 '14 at 17:50