FeRo Posted March 15, 2013 Share Posted March 15, 2013 Hi! I've been working on this thing for hours and I would reeeally appreciate some help... I'm using this Code in my js-File: function runPosition(the_id, left, top){$.get("test.php?id="+the_id+"&left="+left+"&top="+top, {}, function(position) { alert(position);});} the_id, left, and top are just parameters which are being delivered to this function by some other function (they are correct) And the test.php looks like this: <?phpsession_start();$id = $_GET["id"];$top = $_GET["top"];$left = $_GET["left"];$_SESSION[$id] = [$top, $left, $id];return $id;?> But the alert() is completly empty and I don't understand why... Can u help me? Thanks! Link to comment Share on other sites More sharing options...
justsomeguy Posted March 15, 2013 Share Posted March 15, 2013 PHP should echo values, not return them. There's nothing for PHP to return the value to. An ajax request returns the output from the server. Returning a value in PHP does not output the value. You should also execute your PHP code standalone (outside of ajax) to make sure it's going to run, that array bracket syntax is only supported in more recent versions. Link to comment Share on other sites More sharing options...
FeRo Posted March 15, 2013 Author Share Posted March 15, 2013 Thank you for the fast answer!Then how can I get some kind of return value?Or how would you approach this?I'm just trying to get that Session Array back into javascript somehow... Link to comment Share on other sites More sharing options...
justsomeguy Posted March 15, 2013 Share Posted March 15, 2013 Like I said, the ajax response is whatever the page outputted. You can see what the output is if you just pull up the PHP page in the browser, whatever the browser shows is the output. If you want to send an entire array structure to Javascript, then you should use json_encode inside PHP to output the array as a JSON string, and then in Javascript convert the JSON string back to an array. If you look up JSON and PHP then you'll see examples doing that. 1 Link to comment Share on other sites More sharing options...
jeffman Posted March 15, 2013 Share Posted March 15, 2013 (edited) Maybe you meant this: $_SESSION[$id] = "[$top,$left,$id]";echo $_SESSION[$id]; Back in JavaScript, you'll be able to pass that string to JSON.parse() Edited March 15, 2013 by Deirdre's Dad 1 Link to comment Share on other sites More sharing options...
FeRo Posted March 16, 2013 Author Share Posted March 16, 2013 (edited) Like I said, the ajax response is whatever the page outputted. You can see what the output is if you just pull up the PHP page in the browser, whatever the browser shows is the output.Thank you for your replies!But why doesn't it work when I type, instead of the $id, some random echo (echo "whatever"; )?It still alerts nothing(it's empty), but when I open the test.php file itself the text does show up.... Edited March 16, 2013 by FeRo Link to comment Share on other sites More sharing options...
FeRo Posted March 17, 2013 Author Share Posted March 17, 2013 Okay I just had some little syntax error!It's working perfectly now!THANK YOU! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now