0

I'm struggling with the next problem: I have an ajax request that passes a variable to a PHP file. The PHP processes the variable and returns with an array. I need to get back that array so I used the AJAX success callback function but I can only print out the array, nothing more. I want to use that array.

$.ajax({
    method:  'POST',
    url:  "process.php",
    data: { data: data},
    success: function(array)
    {
        <?php echo $GLOBALS['glob']; ?> = data;
    }
});

Even though if I ref to $GLOBALS['glob'] later, it says its an undefined variable. :S please help!

Zong
  • 6,160
  • 5
  • 32
  • 46
egyeneskanyar
  • 167
  • 2
  • 11
  • what is glob variable? – sshet May 12 '14 at 12:00
  • That is supposed to be the global var i want to use later on. – egyeneskanyar May 12 '14 at 12:02
  • 3
    `PHP` = server side. `Javascript(jQuery)` = client side. You can't pass anything to **PHP** through javascript unless using AJAX as it is processed before your client side code. – Darren May 12 '14 at 12:08
  • exact duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Bergi May 12 '14 at 12:16

3 Answers3

1

On succes you assign the variable as "array" and when you try to put it in the php array u say the variable is named "data". that's your first problem.

$.ajax({
    method:  'POST',
    url:  "process.php",
    data: { data: data},
    success: function(**array**)
    {
        <?php echo $GLOBALS['glob']; ?> = **data**;
    }
});

also you try to use php on client side.. normal javascript fires after your page is loaded and the variable is allready filled on page load. in this case you cant use the variable anymore.

And i just noticed this

$.ajax({
    method:  'POST',
    url:  "process.php",
    data: { data: data},
    success: function(**array**)
    {
    <?PHP echo $GLOBALS['glob']; ?> = data 
}
});

You end the variable without assigning it it should be

<?PHP echo $GLOBALS['glob'] = ?> data  <?PHP ; ?>

but as i said php scripts are executed before javascript so when the server reads the file it only says

echo $GLOBALS['glob'] = ;

Sjoerd de Wit
  • 2,353
  • 5
  • 26
  • 45
0

Try sending a json array from your business logic.

0

IN your php file you need to return json string and in success callback function you can get the object you are passing through server side

Like in php file, after all processing at the end convert the array to json string

json_encode($array);

and in your success call back function try to debug console.log(array)

sshet
  • 1,152
  • 1
  • 6
  • 15
  • I do get the array, or the array content if I want, still I want to copy the array into a PHP array or something, so I will be able to use this array not just write out in process.php and callback – egyeneskanyar May 12 '14 at 12:14
  • So why not you assign this array to your global var in php file itself instead of returning back as a array – sshet May 12 '14 at 12:17
  • the process.php file is a different file and I need the array in my other php file where the AJAX request is located. – egyeneskanyar May 12 '14 at 12:20