4

I am trying to learn the Jquery AJAX function but am struggling to work out how to pass PHP variables into my main document.

This is what I currently have:

<script>
  var refreshId = setInterval(function() {
    $.ajax({
      url: "test.php",
      dataType: "json", //the return type data is jsonn
      success: function(data){ // <--- (data) is in json format
        $('#testdiv').html(data.test1);
        $('#testdiv').append(html(data.test2));
        //parse the json data
      }
    });

}, 1000);
</script>
4
  • 2
    Someone should be along to answer you question in detail. But you could start looking at getJSON on api.jquery.com. And use the PHP method json_encode to encode and send the data back to you Commented Apr 18, 2011 at 8:14
  • Hi JohnP, thanks for your reply. I have briefly looked at the getJSON page but I find it hard to understand from the Jquery website. Would you mind modifying my code to do this so I can learn from what you do please? Commented Apr 18, 2011 at 8:18
  • @user683526 look at what user experimentX has done. That is what you need. json_encode on the PHP side and a json call on the JS side Commented Apr 18, 2011 at 8:20
  • 1
    Invaid function html(data.test2) instead just try $('#testdiv').html($('#testdiv').html() + data.test2); Commented Apr 18, 2011 at 8:54

5 Answers 5

5

You should use json or xml format and parse it, and get the variable.

<script src="jquery.js"></script>
<script>
    $.ajax({
      url: "test.php",
      dataType: "json", //the return type data is jsonn
      success: function(data){ // <--- (data) is in json format
        alert(data.test1);
        //parse the json data
      }
    });
</script>

on test.php

<?php

$test = array();
$test['test1'] = '1';
$test['test2'] = '2';
$test['test3'] = '3';

echo json_encode($test);
//echo nothing after this //not even html
Sign up to request clarification or add additional context in comments.

8 Comments

Hi experimentX, thanks for your help. I have tried adding this into my pages but now no output is displayed at all, I have probably done it wrong. Would you mind having a look at my updated post to see what needs changing please?
@user683526 would you mind changing your display name. sure i will have a look and update.
@experimentX: Why is important to not have anything after that echo? What if that echo is inside a conditional... if(..) echo json_encode($test), else echo json_encode($test2). I know you could do a final echo, but I would like to know why you can't add anything after echo json_encode(...). Thanks :)
Thanks experimentX :) I'm struggling to understand but this really helps. Is it possible to place the data.test1 into a div and then update it periodically?
@DanielH yup, for that you need to call this ajax function periodically, and update the value in div $('#somediv').html(data.item1)
|
1

use dataType='json' in ajax option on index.php

and on test.php use json_encode

$ret_array= array ($test1, $test2 and $test3);
echo json_encode($ret_array);

again on index.php

now if u want to use it in your *J*S file then access via object navigation or using getJSON method

and if u want to use that data directly in php then use json_decode

json_decode($ret_array,true);

References

json_encode
getJSON
json_decode

3 Comments

The question deals with how to get the 3 variables out of test.php and into the JS vars in index.php. json_decode would not be useful there since it's a PHP method
okay but after sending data on indx.php he can access via simple object notation..m i correct??
After sending data from test.php to index.php he can access it via object notation. That's what you need to include in your answer :)
1

Another approach (with ajax but hidden field) would be:

$.ajax({
    type: 'POST',
    url: "test.php",
    dataType: "json", //the return type data is jsonn
    success: function(data){ // <--- (data) is in json format
        $('#testdiv').html('<input type="hidden" value="' + data.test1 + '" />');
    }
    error: ajaxError,
    dataType: "html"
});

With that inside of your form you can even use your value in the next Postback without passing it directly.

5 Comments

What's that negative comment? If he want's to get it from javascript the hiddenfield is an appropriate oppinion. When the hiddenfield is loaded via ajax he can access it so where's the problem???
I downvoted because the OP doesn't know how to do it via AJAX. So your answer doesn't actually solve the question. If you'd update your answer so that it includes the AJAX way of doing it, I'd be happy to upvote. Your reply has to at least deal with the part of getting the data to the hidden field via AJAX
I edited and hope you're satisfied with this. My resolution was supposed to be inside the ajax-request but I just forgot to mention it. May I apologize?
@wegginho this answer is totally fine. Hope you understand my reasons for downvotng.
first of all I thought you wouldn't understand what I meant, a day after that I didn't understand what I meant, so sure and thanks!
-1

You don't pass them into index.php, but you can add them as content that is loaded by your ajax something like this:

$test1 = '1';
$test2 = '2';
$test3 = '3';

echo $test1 . "<br/>" . $test2 . "<br/>" . $test3 . "<br/>";

1 Comment

You misunderstood. He wants to read back the output from test.php and get at them as JS variables
-1

Unless im greatly mistaken its as simple as:

<?php
  include 'test.php';
?>

To include the test.php file in index.php

Then you can call them as if they were variables set in index.php, i would then do the following to get them into jQuery:

$("#test1") etc...

Update

The problem I have is that the variables in test.php will be changing constantly, and then these need to be updated on index.php. I only set them as 1, 2 and 3 to test it out. – user683526 1 min ago

In that case set them in a function and return the values.

3 Comments

No, you've misunderstood. He wants to pass the variables from test via the AJAX call he's doing on index.php
The problem I have is that the variables in test.php will be changing constantly, and then these need to be updated on index.php. I only set them as 1, 2 and 3 to test it out.
Okay cool, he should maybe phrase the question better > "Could someone please explain how I would pass the 3 varaibles $test1, $test2 and $test3 into index.php so I can call them on the page?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.