0

I have a page that loads in "get-results.php" which loads the results into a div. This works great. However, I also need to grab a variable from the php file that is being loaded, so that I can re-include it in the .load() url, this way I can have the php file have the previous result for comparison.

$(".results").load("get-results.php" + "?userClicked=" + var1 + var2 + "&prevresult=" + theResults);

In the "get-results.php" file, I have set

var theResults = "<?php echo $result; ?>";

Is there a way for me to access "var theResults" from the page that is loading it (outer page, not inner)? How can I get the Var from the file and put it into a Var in the outer file? Any pointers would be great thanks.

4

2 回答 2

1
    var theresults;
    $(".results").load("get-results.php" + "?userClicked=" + var1 + var2 + "&prevresult=" + theResults,function() {
        theresults=$(".results").html();
       }));

obviously you have to wait the load is complete to get the new value for theresults. otherwise, you can use $.get which seems more appropriate here :

      $.get("get-results.php" + "?userClicked=" + var1 + var2 + "&prevresult=" + theResults,function(data) {
         theresults=data;
         $(".results").html(data);
   }))

edit : if you are indeed sending more than this php var, you should send back a json (with your variables to be sent in an array then you echo json_encode($arrayofvar) then in the js you'll retrieve a json object of your var in the getJSOn callback

于 2013-02-09T05:50:30.133 回答
0

No you can not, i.e if you are sending other things with the variable.

If you are sending only that variable ( which I think you are not), then use the above answer.

Else

try this one:

in php file:

var theResults = "<?php echo $result; ?>";
echo "<input type='hidden' value='".$theResults ."' id='theResults'/>";

in javascript file:

$(".results").load("get-results.php" + "?userClicked=" + var1 + var2 + "&prevresult=" + theResults,function() {
        theresults=$("#theResults").val(); alert(theresults);
       }));
于 2013-02-09T05:54:43.350 回答