Forum Moderators: open

Message Too Old, No Replies

Passing Javascript variables from jQuery $.get() back to parent PHP

         

csdude55

10:44 am on Feb 14, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sorry to be making so many posts lately! I'm new to jQuery, though, and I'm starting to have a little fun with programming again :-)

Today's issue...

I'm calling a PHP script via jQuery, and in that PHP script I define several JavaScript variables. I would then like to use those JavaScript variables back in the parent PHP script. These variables are ones that (1) rely on PHP to find the values, and (2) are only used in mouseover events so I can afford for them to load a little later in the script so as to not slow down rendering time.

My first thought was to simply use:


<script>
var x = 'test'; // set globally

$.get('whatever.php', function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});

alert(x);
</script>


And then in whatever.php:


<script>
x = 'example';
</script>


But of course this shows 'test', not 'example'. I get the "success", "second success", and "finished" alerts, so I know the PHP is loading correctly, I just don't store any of the variables.

I've tried using $.get, $.load, and $.ajax, and still haven't had the result I'm looking for. I've done several Google searches, but everything I'm finding is about sending variables TO the jQuery, not how to get them back.

I'm sure that I can keep plugging away tomorrow (Sunday) and figure it out, but before I waste an ENTIRE day, I thought you guys might be able to suggest a way off the top of your head(s) to do this?

oligalma

9:26 pm on Feb 14, 2016 (gmt 0)



You can use json_encode() to send variables back, and you can collect them using the data variable:

PHP:

$response = array('var1' => 'aaa', 'var2' => 'bbb');
echo json_encode($response);


JS:

$.ajax({ 
type: "GET",
url: "myurl.php",
data: "FirstName="+ $('#name').val(),

success: function(data)
var data = $.parseJSON(data);
alert(data.var1);
}
});

csdude55

4:57 am on Feb 16, 2016 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I appreciate the reply, oligalma, but I couldn't figure out how to use what you posted :-( In whatever.php (the script loaded by Ajax), I don't think it's possible to get the JavaScript variable passed back to the PHP array.

This is obviously fake code, but how would one make it actually work? I'm sure that the PHP code would be processed before the JavaScript code, so I don't think it's possible.


<?php
$data <<<EOF
<script>
var js_var = 'example';
var js_var_2 = 'tester';
<script>

EOF;

$response = array('js_var' => js_var, 'js_var_2' => js_var_2);
echo json_encode($response);
?>



After some playing around, I found that this will return the raw data as a string:


// whatever.php
echo <<<EOF
js_var = 'example';
js_var_2 = 'tester';

EOF;

// main.php
$.get("whatever.php", function(data) {
alert(data);
});


So it appears that I end up with a string like:

data = "js_var = 'example';
js_var_2 = 'tester';";


That's halfway there, but I still have to manually parse data, like:


var vars = data.split(';');
var query = {};

for (var i = 0; i < vars.length; i++) {
var b = vars[i].split('=');
query[decodeURIComponent(b[0])] = decodeURIComponent(b[1] || '');
}


This doesn't let me use js_var, though; I have to use query['js_var']. Which isn't ideal, but I think I can make it work.