How to use ajax response data in php

I am having a problem with response data. I want to use the response data in my php file. I want to assign them to a php variable.

Here is the ajax code in the insert.php file.

function change(){ var tp=$('#choose').val(); var country_val=$('#country_filter').val(); //alert(country_val); //var country_val=$('#country_filter').val(); $.ajax({ type: "POST", url: "filter.php", data: { name: tp, country:"test"}, success:function( data ) { alert(data); } }); } 

here is the php code in the filter.php file

 if($_REQUEST["name"]){ $lvl = $_REQUEST["name"]; //global $lvl; echo $lvl; } 

Now I want to use the response data that will be assigned to the php variable in the insert.php file. How can i do this? Please, help.

+4
source share
2 answers

Send the data to the insert.php file using ajax, instead of warning it.

change the success function to

 $.ajax({ type: "POST", url: "filter.php", data: { name: tp, country:"test"}, success:function(response) { var res = response; $.ajax({ type: "POST", url: "insert.php", data: { res: res }, success:function(data){ alert(data); } }); }); 

In insert.php,

use this to get the variable.

 $var = $_POST['res']; 
+1
source

If insert.php calls filter.php with ajax, then returns a value. you cannot apply this javascript var to php var in insert.php. This is because the php script runs on the server when the page loads, and javascript runs locally on the user's computer. To apply anything to the php variable, you have to reload the page.

0
source

All Articles