tags:

views:

116

answers:

3

I'm doing an AJAX call using jQuery's JSON feature.

function get_words_json(address, username, paging_value) {
 $.ajax({
  type: "GET",
  url: "json/" + address,
  dataType: "json", 
  data: "username=" + username + "&paging_no_st=" + paging_value,
  success: function(json){
   pop_word_list(json, paging_value);
  }
 });
}

As you can see, I'm sending the response to another JavaScript function, but what I'd like to do is send the response to PHP. Is this possible, say to directly convert the response into a PHP array (not using JavaScript) and then use PHP to handle the array, etc?

Thanks in advance.

+2  A: 

You could perform another Ajax call to the php script in the success function, passing along the JSON data as a POST param.

Jacob Relkin
exactly what I was wirtting
Elzo Valugi
This is risky, as the user might visit another page while the first ajax call is being executed, and so the second 'pass' will never execute as the response will not get handled by the client .. It is not safe to rely to the client..
Gaby
I doesn't concern the task if the response is not handled by the client, so this seems like a solution. Thanks.
tim
@tim, if it's a solution, mark it as the 'accepted answer'.
Jacob Relkin
A: 

do this?

js (ajax) -> php (array conver to ajax) -> js (ajax) -> php ?



function get_words_json(address, username, paging_value) {
 $.ajax({
  type: "GET",
  url: "json/" + address,
  dataType: "json", 
  data: "username=" + username + "&paging_no_st=" + paging_value,
  success: function(json){
   json["paging_value"] = paging_value;
   $.post("x.php", json);
  }
 });
}
andres descalzo
A: 

The whole idea doesn't stick together at all... but:

  1. If there is a reason to do that - then You want to do the $.post('phpfile.php',json,function(){},'text or whatever type You want in return'); and the whole json object goes to PHP's $_POST[] as suggested above, but I can see NO case where it should be done that way.

  2. If You get that json from some code You can't change and want to use data in php do:

    • use cURL to get the data from another thing
    • use json_decode($data,true) to get assoc table of the whole thing
  3. If You don't know what You're doing :)

    • just pass the object to another function without useless sending stuff back and forth. You might want to do empty AJAX call to trigger the php file, nothing more.
naugtur