var1 và var2 are javascript variables on the same page. I know client side variable cannot be passed to server side. But is there any workaround as the variables are in the same page.
Bạn đang xem: How do i pass javascript variables to php

Trending sort Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping khổng lồ surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch khổng lồ Trending sortHighest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
In order for you khổng lồ make this happen - I believe - you have to use AJAX.
The code will look like this:
$.ajax( url: "your_script.php", type: "POST", data: var1: javascript_var_1, var2: javascript_var_2, success: function(data) console.log("success"); );Your PHP will look similar lớn this (without keeping in mind the JSON encode:
Then you can JSON encode the results & pretty much output đầu ra them on the success. Your php script - however - must live on another php file.
Also, escape your data. Use prepared statements.
Xem thêm: Bầu Chửa Bụng Trên Là Trai Hay Gái Phổ Biến Giúp Ba Mẹ Dự Đoán Giới Tính Con
share
Follow
answered May 2, năm trước at 19:52

JJPPJJPP
76922 gold badges1010 silver badges2626 bronze badges
2
showroom a phản hồi |
1
In theory, you could pass the vars lớn php via some sort of ajax call. I think it would go something like...
JavaScript:
var data = "var1": $("selector").val(), "var2": $("selector").val();$.ajax( type: "post", url: "path-to-your-file.php", data: data, timeout: 50000).done(function(response) console.log(response);).fail(function(error) // uh-oh.);php:
Otherwise, you could use:
cookie hidden inputphp:
NOTE: you will want khổng lồ sanitize the data. Using raw cookie and/or input đầu vào values could lead to lớn some less than desirable results.
share
Follow
edited May 2, năm trước at 20:23
answered May 2, năm trước at 19:57

DamonDamon
3,65277 gold badges3838 silver badges8787 bronze badges
add a bình luận |
0
Use Method Post in AJAX or Form!! to lớn send js variable in Php & receive it in php using $_post< name_of_obj >;
giới thiệu
Follow
answered May 2, năm trước at 19:56

ashbuildsashbuilds
1,4011616 silver badges3232 bronze badges
địa chỉ cửa hàng a bình luận |
Your Answer
Thanks for contributing an answer khổng lồ Stack Overflow!
Please be sure to lớn answer the question. Provide details & share your research!But avoid …
Asking for help, clarification, or responding khổng lồ other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.
Draft saved
Draft discarded
Sign up or log in
Sign up using Google
Sign up using Facebook
Sign up using email and Password
Submit
Post as a guest
Name
thư điện tử Required, but never shown
Post as a guest
Name
thư điện tử
Required, but never shown
Post Your Answer Discard
By clicking “Post Your Answer”, you agree to lớn our terms of service, privacy policy and cookie policy
Not the answer you're looking for? Browse other questions tagged javascript php mysql or ask your own question.
The Overflow Blog
Featured on Meta
Linked
2
How to lớn create a select list with javascript and populate it with php sql?
Related
1371
How can I đầu ra MySQL query results in CSV format?
1655
Is JavaScript a pass-by-reference or pass-by-value language?
1986
How bởi I check if a variable is an array in JavaScript?
2695
How can I get query string values in JavaScript?
4944
Reference — What does this symbol mean in PHP?
2467
kiểm tra if a variable is a string in JavaScript
2211
JavaScript kiểm tra if variable exists (is defined/initialized)
1468
JavaScript phối object key by variable
751
How vì chưng I pass variables và data from PHP lớn JavaScript?
Hot Network Questions more hot questions
Question feed
Subscribe to RSS
Question feed lớn subscribe to this RSS feed, copy và paste this URL into your RSS reader.

default
Stack Overflow
Products
Company
Stack Exchange Network
Site thiết kế / logo © 2022 Stack Exchange Inc; user contributions licensed under CC BY-SA. Rev2022.8.2.42721
Your privacy
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & disclose information in accordance with our Cookie Policy.