使用 MySQL 和 PHP 将 javascript 变量传递到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9651366/
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 variables into Database with MySQL and PHP
提问by jQuerybeast
Possible Duplicate:
How to pass a variable / data from javascript to php and vice versa?
I have 3 javascript variables which gives me an id, a name and a surname as such:
我有 3 个 javascript 变量,它们给了我一个 id、一个名字和一个姓氏:
if (!response.error) {
document.getElementById("meName").innerHTML = response.id
+ " " + response.name
+ " " + response.surname ;
}
Now I want to pass those variables (response.id
, response.name
and response.surname
) into my database.
现在我想将这些变量 ( response.id
,response.name
和response.surname
)传递到我的数据库中。
Something like this:
像这样的东西:
<?php
$query = mysql_query("INSERT INTO users (id, name, surname) VALUES
('response.id', 'response.name', 'response.surname')") or die(mysql_error());
$result = mysql_fetch_array($query);
return $result;
?>
How can I do this?
我怎样才能做到这一点?
回答by jQuerybeast
This is what you need:
这是你需要的:
if (!response.error) {
var uid = response.id;
var firstname = response.name;
var surname = response.surname
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
xmlhttp.open("GET", "ajax.php?uid=" + uid + "&firstname=" + firstname + "&surname=" + surname , true);
xmlhttp.send();
return false;
}
and in your ajax file:
并在您的 ajax 文件中:
<?php include("YOUR_CONNECTION_FILE.php");
$uid = mysql_real_escape_string($_GET['uid']);
$firstname = mysql_real_escape_string($_GET['firstname']);
$username = mysql_real_escape_string($_GET['surname']);
$query = mysql_query(" YOUR MYSQL QUERY ") or die(mysql_error());
?>
回答by rks
I'm guessing you're trying to pass the javascript variables to php.
我猜你正试图将 javascript 变量传递给 php。
In order to do this you need to use a POST or GET method.
为此,您需要使用 POST 或 GET 方法。
回答by Alex
Well, ajax would be the best choice in your case.
好吧,ajax 将是您的最佳选择。
Check my answer here How to send a form without refreshing the page?
在此处查看我的答案如何在不刷新页面的情况下发送表单?
Check out some documentation also:
还可以查看一些文档:
jQuery Documentation
jQuery 文档
Tutorials
教程
回答by Amber
You'd need to make an AJAX call to a separate PHP script which stores the variables.
您需要对存储变量的单独 PHP 脚本进行 AJAX 调用。
Javascript runs browser-side, whereas PHP runs on the server - by the time the browser is running the Javascript code in the page, it has already finished running on the server and thus all of the PHP code has already executed.
Javascript 在浏览器端运行,而 PHP 在服务器上运行——当浏览器在页面中运行 Javascript 代码时,它已经在服务器上运行完毕,因此所有 PHP 代码都已经执行。
回答by Eduardo Lagares
I believe that response is your return via ajax, if so, you should check two things
我相信响应是您通过 ajax 返回的,如果是这样,您应该检查两件事
First, set the ajax dataType to 'json'.
首先,将 ajax 数据类型设置为 'json'。
Second, replace
二、更换
return $result
to
到
return json_encode($result);
By.
经过。