开发者

jQuery posting variables within the same file

开发者 https://www.devze.com 2023-03-17 10:13 出处:网络
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 a

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 开发者_StackOverflow中文版current page e.g. index.php?

Thanks very much for your help.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号