使用后续php段落中脚本返回的var

Presently,

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript">
    $.get("http://[somewebsite]", function (response) {
       $("#somevalue").html(response.somevalue);
    }, "jsonp");
</script>

up in the body of the source returns a somevalue variable that is then used like this within the page:

<div id="somevalue"></div>

and the underlying value is successfully printed out. But down below further in the source I have some PHP coding that I would like to employ that variable in. If in that later PHP coding I want to take <div id="somevalue"></div> (merely writing somevalue just outputs string literal "somevalue" in the html), and assign the value to n for use inside the PHP, how is such an instruction written? Thanks for any help.

I would load that PHP part later via an Ajax request as well. In this way you can pass the 'somevalue' as a parameter (GET or POST) and you can process it on the server-side.

$.get("http://[somewebsite]", function (response) {
    $("#somevalue").html(response.somevalue);
    loadSomething(response.somevalue);
}, "jsonp");


function loadSomething(somevalue) {
    $.get('/later.php', { n: somevalue }, function(response) {
        // Inject the response into the DOM or do something else
    });
}

UPDATE: On re-reading your question, I'm thinking you actually wanted to use the value as a variable in PHP? That can't be easily done unfortunately, as PHP is handled on the server, before the JavaScript is executed. You could try using the cURL functions to get the value you want independently of your AJAX?


It's a little hacky, but you could try replacing your top part with this:

var outputHTML = '';
$.get("http://[somewebsite]", function (response) {
   outputHTML = response.somevalue;
   $("#somevalue").html(outputHTML);
   $(".somevalue-later").html(outputHTML);
}, "jsonp");

And then when you want to use it later, use this:

<?php
echo("Blah blah blah <span class=\"somevalue-later\"></span> blah blah");
?>

That should all work, but as stated at the top I suspect that's not what you were looking for in the first place anyway. If you can expand on your question I'm sure I or others will be able to help you.