ajax success function alert data but on php side no data there GET array is empty
<script type="text/javascript">
var array={
name:"amr",
age:22
}
array =JSON.stringify(array);
$.ajax({
url :"new.php",
type : "GET",
data : {action:array},
success: function(response){
alert(response);
},
});
</script>
<?php
$var =json_encode($_GET['action']);
echo $var;
?>
The PHP code is executed on the server, the output of PHP is then sent to the client, the client executes JavaScript, not PHP. In your example you have two pages, ajax.php and new.php, in this example you don't need to use PHP in ajax.php page, since once the PHP present in ajax.php is executed on server and the output is sent to the client it will not change anymore.
This code:
<?php
$var =json_encode($_GET['action']);
echo $var;
?>
should be into your new.php file, and only there, ajax.php will call from the client new.php, that will execute your PHP and will send back the output to the client, once you have the output on the client you can do with it whatever you want, for example this may be the source code in your ajax.php:
<script type="text/javascript">
var array={
name:"amr",
age:22
}
array =JSON.stringify(array);
$.ajax({
url :"new.php",
type : "GET",
data : {action:array},
success: function(response){
/*
* This will write on your page (ajax.php) the response
* from the server, note that this is done with javascript,
* not with PHP
*/
document.write(response);
},
});
</script>
You can also rename the file ajax.php to ajax.html, since no PHP is needed on this page.