php 使用 jQuery/AJAX 更新 MYSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10292452/
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
Update MYSQL with jQuery/AJAX
提问by user1323294
I'm trying to build a mobile application with PhoneGap and jQuery Mobile . In my app I have a page where is a link to PHP file which updates MYSQL and heads to next page. But with PhoneGap I need to have all the PHP files on external server so I can't my current solution on this application.
我正在尝试使用 PhoneGap 和 jQuery Mobile 构建移动应用程序。在我的应用程序中,我有一个页面,其中有一个指向 PHP 文件的链接,该文件更新 MYSQL 并转到下一页。但是对于 PhoneGap,我需要在外部服务器上拥有所有 PHP 文件,因此我无法在此应用程序上使用当前的解决方案。
This is the PHP that I use to update MYSQL
这是我用来更新 MYSQL 的 PHP
<?php
$var = @$_GET['id'] ;
$con = mysql_connect("localhost","username","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
mysql_query("UPDATE table SET condition=true
WHERE ID= \"$var\" ");
header('Location: http://1.2.3.4/test');
mysql_close($con);
?>
So how can I run this PHP when user clicks a button? With jQuery/AJAX i suppose?
那么当用户单击按钮时如何运行这个 PHP 呢?我想用 jQuery/AJAX 吗?
回答by skos
Lets say the above PHP code is in file, update.php. Then you can use the following code -
假设上面的 PHP 代码在文件 update.php 中。然后你可以使用以下代码 -
<head>
<script src="jquery.js"></script>
<script>
function UpdateRecord(id)
{
jQuery.ajax({
type: "POST",
url: "update.php",
data: 'id='+id,
cache: false,
success: function(response)
{
alert("Record successfully updated");
}
});
}
</script>
</head>
<body>
<input type="button" id="button_id" value="Update" onClick="UpdateRecord(1);">
</body>
Just pass a valid id in the UpdateRecord function. Put your PHP code in update.php file. Just to be on a safer side, in your PHP code replace $var = @$_GET['id'] ;with $var = @$_POST['id'] ;and check if this works for you
只需在 UpdateRecord 函数中传递一个有效的 id。将您的 PHP 代码放在 update.php 文件中。只是要在安全方面,在PHP代码代替$var = @$_GET['id'] ;与$var = @$_POST['id'] ;和检查,如果这对你的作品
回答by nodeffect
<head>
<script src="jquery.js"></script>
<script>
function UpdateRecord(id)
{
jQuery.ajax({
type: "POST",
data: 'id='+id, // <-- put on top
url: "update.php",
cache: false,
success: function(response)
{
alert("Record successfully updated");
}
});
}
</script>
</head>
<body>
<input type="button" id="button_id" value="Update" onClick="UpdateRecord(1);">
</body>
Those codes suggested by Sachyn Kosaredoes work, the only thing is that the line data: 'id='+idhas to be on top of url: "update.php"
Sachyn Kosare建议的那些代码确实有效,唯一的问题是该行data: 'id='+id必须在url: "update.php"
And, you can use $var = $_POST['id'];for your PHP.
并且,您可以将其$var = $_POST['id'];用于您的 PHP。
回答by Baz1nga
say your button has an id clickmeyou bind an event handler on it as follows, I am usnig the on selector to avoid cases where the code is executed when the button does not exist on the page.. you can very well bind a clickhandler
说你的按钮有一个 idclickme你绑定一个事件处理程序如下,我使用 on 选择器以避免当页面上不存在按钮时执行代码的情况..你可以很好地绑定一个click处理程序
$(document).on("click","#clickme",function(){
$.ajax({
type:"POST", //GET - update query should be POST
url: my_endpoint.php, //your php end point
data: {jsonKey1:jsonValue1},
success: function(data){ //if success
//do necessary things with data
}
})
});
Is this what you are looking for?
这是你想要的?
回答by Muhammad Raheel
Do something in your success function
在你的成功职能中做一些事情
success: function(response)
{
window.location = response;
}
and you html should contain
你的html应该包含
And in php do something like this
在 php 中做这样的事情
mysql_query('blah blah');
mysql_close($con);
echo "http://1.2.3.4/test";

