Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
ajax call successful but no values in php

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;
?>

enter image description here

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!