javascript 将javascript变量传递给php mysql选择查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23435808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pass javascript variable to php mysql select query
提问by nyxem1
I am running a mysql select query in php like this
我正在像这样在 php 中运行 mysql 选择查询
<?php
$getvalue="SELECT id,name from table1 WHERE column1='$var1' and column2='$var2'";
$result=mysql_query($getvalue) or die(mysql_error());
while($row=mysql_fetch_array($result)){
extract($row);
echo $name;
}
?>
var1 and 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.
var1 和 var2 是同一页面上的 javascript 变量。我知道客户端变量不能传递给服务器端。但是是否有任何解决方法,因为变量在同一页面中。
回答by Tiffany Lowe
In order for you to make this happen - I believe - you have to use AJAX.
为了让你做到这一点 - 我相信 - 你必须使用 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 to this (without keeping in mind the JSON encode:
您的 PHP 将与此类似(无需记住 JSON 编码:
<?php
$var1 = $_POST['var1'];
$var2 = $_POST['var2'];
$getvalue="SELECT id,name from table1 WHERE column1='$var1' and column2='$var2'";
$result=mysql_query($getvalue) or die(mysql_error());
while($row=mysql_fetch_array($result)){
extract($row);
echo $name;
}
?>
Then you can JSON encode the results and pretty much output them on the success. Your php script - however - must live on another php file.
然后你可以对结果进行 JSON 编码,并在成功时输出它们。您的 php 脚本 - 但是 - 必须存在于另一个 php 文件中。
Also, escape your data. Use prepared statements.
此外,转义您的数据。使用准备好的语句。
回答by Damon
In theory, you couldpass the vars to php via some sort of ajax call. I think it would go something like...
理论上,您可以通过某种 ajax 调用将 vars 传递给 php。我认为它会像......
JavaScript:
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:
php:
<?php
print_r($_POST['data']);
Otherwise, you could use:
否则,您可以使用:
- cookie
- hidden input
- 曲奇饼
- 隐藏输入
php:
php:
<?php
... column1='$_COOKIE["mycookie1"]' and column2='$_COOKIE["mycookie1"]'";
... column1='$_POST["hidden1"]' and column2='$_POST["hidden2"]'";
NOTE:you will want to sanitize the data. Using raw cookie and/or input values could lead to some less than desirable results.
注意:您需要清理数据。使用原始 cookie 和/或输入值可能会导致一些不太理想的结果。
回答by ashbuilds
Use Method Post in AJAX or Form!! To send js variable in Php and receive it in php using $_post[ name_of_obj ];
在 AJAX 或 Form 中使用 Method Post!!在 PHP 中发送 js 变量并在 php 中使用 $_post[ name_of_obj ] 接收它;