0

I have some variables set in Javascript. Further down the script I want to use these values in PHP. I realise I need to POST/GET them with jQuery, but I don't understand either function fully, even after looking at the manuals.

  1. Could somebody break it down and explain the parameters?
  2. Would I be better off using GET or POST in the instance?
  3. Can the URL specified be the same as the current page e.g. index.php?

Thanks very much for your help.

2 Answers 2

1

You can not do this unless PHP is writing the javascript. PHP is on the server side and will be parsed before Javascript is ever seen by the client. Any variables set by JS will NOT be seen by PHP on the same request.

Sign up to request clarification or add additional context in comments.

3 Comments

Nope. jQuery is just a cleaner more cross-browser friendly way to code JavaScript. No super powers ;)
You could, or you could have PHP write the JavaScript for you and then you could use PHP variables in place of JavaScript variable 'values'. This is very common. Say you had a common.js file. Now say you have a common.js.php file instead - PHP echos all the data with a header type of text/javascript, and you can embed all the PHP data in your JS that you need.
That sounds promising, would it involve echoing all of the JS in PHP and then doing something along the lines of $lat = <script>position.coords.latitude</script>;
0

It's really just a question of style, really.

GET places all key/value-pairs in the URL field, whereas POST puts it in the HTTP body. Since URLs are limited in length, POST is preferred for longer, larger sets of data or data needing to benefit from TLS/SSL encryption.

So let's say we have a key: articleID. You want to pass 1 to articleID, so that the backend can contact the database and retrieve the article in question.

If you make a GET request, you'd invoke the following URL:

index.php?articleID=1

If you use POST, you'll put the data in the request body itself, so you wouldn't be able to tell what value you sent to the server without opening the packet in question and examining the request.

You'll find more information on how to perform these requests back at jQuery's reference site. More information about GET and POST.

You are the architect of the application, so you would know best what method to use. As for contacting the view itself, it's certainly possible albeit questionable from an architectural point of view.

4 Comments

Thanks for your reply. How would I go about sending two JS variables via GET? My best guess is $.get("location.php", {lat, long});
You're welcome! $.get('index.php', {lat: 0.00, long: 0.00}) More information, please go here: api.jquery.com/jQuery.get
This keeps my JS valid, but when I try and bring the variable into PHP further down the page using $lat = $_GET['lat']; I get an error. Any ideas?
There will be cases when $_GET['lat'] will be undefined, like the first time you open the page without query string parameters. You need to make sure that it exists before you try to retrieve it. You can do this with isset: if (isset($_GET['lat']) && isset($_GET['long'])) { /* your logic */ } I hope it'll work out for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.