#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0

    Sending PHP variables to Javascript using AJAX


    Hi,

    I want to use an AJAX query to obtain information dynamically from a PHP page. Now, I want to send information from the PHP request to my main open web page; how do I send this information (in the server side script that I call) and how should I interpret this client side?

    For example, what I can do is on the PHP request have a result printed something like this (where $result is an array):
    PHP Code:
    print json_encode($result); 
    So, now I have an array printed at the php webpage in json format; now when I try to load it in the ajax function:

    Code:
    success:function(result){
    
                output = JSON.parse(result);
                $("div").html(output);
    
    }
    it doesn't give any output.Basically I would like to have an array in the php script, then return it in a way that I can send it to AJAX and then when AJAX has received it I would like to edit it in a way again so that I can use it as an array again, with the same dimensions and content as I originally had in the PHP request. Any idea how to go about this?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,998
    Rep Power
    375
    1. what happens if you go to that page direcly, do you actually know if there is a result?

    for debug: just do alert (result) to see what you get. You might just get an alert of "object" in which case, just print the $result on php page rather than encoding it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by paulh1983
    1. what happens if you go to that page direcly, do you actually know if there is a result?

    for debug: just do alert (result) to see what you get. You might just get an alert of "object" in which case, just print the $result on php page rather than encoding it.
    Thanks, it's working now, I had to call the elements of the array separately with the output method I was using.

IMN logo majestic logo threadwatch logo seochat tools logo