Jump to content

jQuery $.get()


FeRo

Recommended Posts

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

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

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.

  • Like 1
Link to comment
Share on other sites

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 by Deirdre's Dad
  • Like 1
Link to comment
Share on other sites

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 by FeRo
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...