Javascript 使用ajax请求更新数据库值
我有一个表单,它有Javascript 使用ajax请求更新数据库值,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我有一个表单,它有单选按钮,用户必须单击这些按钮来增加数据库中的值,但我单击单选按钮,数据库中没有任何事情发生。我的表单代码如下: <form id="myform" name="myform" method="post"> <div class="radio show-options"> <li><label id="l_security"><input type="radio" id="r_security" name="wee
单选
按钮,用户必须单击这些按钮来增加数据库中的值,但我单击单选
按钮,数据库中没有任何事情发生。我的表单代码如下:
<form id="myform" name="myform" method="post">
<div class="radio show-options">
<li><label id="l_security"><input type="radio" id="r_security" name="weekend" value="security" />Security</label> (<label id="c_security">0</label>)</li>
</div>
<div class="radio show-options">
<li><label id="l_manager"><input type="radio" id="r_manager" name="weekend" value="manager" />Manager</label> (<label id="c_manager">0</label>)</li>
</div>
<div class="radio show-options">
<li><label id="l_cleaner"><input type="radio" id="r_cleaner" name="weekend" value="cleaner" />Cleaner</label> (<label id="c_cleaner">0</label>)</li>
</div>
</form>
保安(0)
经理(0)
清洁剂(0)
这里是表单的脚本
<script type="text/javascript">
var lastClicked = '';
function getTotals() {
// function to get click counts as JSON from helper page
// expects get_count.php to be in same directory level
$.ajax({
type: "GET",
url: "get_count.php",
dataType: "json",
error: function(xhr, status, msg) {
alert("Failed to get click counts: " + msg);
}
})
.done(function(data) {
// loop through JSON variables, assign to count labels
$.each(data, function(key, value) {
var tmp = "#c_" + key;
$(tmp).text(value);
});
});
}
function processClick(obj) {
// function to increment click count via ajax
// expects increment_count.php to be in same directory level
if(lastClicked != obj.val()) { // don't count clicks on currently active radio button
lastClicked = obj.val(); // set currently clicked radio button to this one
var qs = "weekend=" + obj.val(); // set query string value
$.ajax({
type: "GET",
url: "increment_count.php",
data: qs,
error: function(xhr, status, msg) {
alert("Failed to process click count: " + msg);
}
})
.done(function() {
getTotals(); // update totals on successful processing
});
}
}
$(document).ready(function() {
getTotals(); // get click totals on initial page load
$(document).ready(function() {
// add click incrementing event handler to all radio buttons on page
$('input:radio').click(function() {
processClick($(this));
});
});
});
</script>
var lastClicked='';
函数getTotals(){
//函数从帮助器页面获取作为JSON的单击计数
//希望get_count.php位于同一目录级别
$.ajax({
键入:“获取”,
url:“get_count.php”,
数据类型:“json”,
错误:函数(xhr、状态、消息){
警报(“未能获取点击次数:“+msg”);
}
})
.完成(功能(数据){
//循环JSON变量,分配给计数标签
$。每个(数据、函数(键、值){
var tmp=“#c#”+键;
$(tmp).文本(值);
});
});
}
函数processClick(obj){
//通过ajax增加点击次数的函数
//期望increment_count.php位于同一目录级别
如果(lastClicked!=obj.val()){//不计算当前活动单选按钮上的点击次数
lastClicked=obj.val();//将当前单击的单选按钮设置为此单选按钮
var qs=“weekend=“+obj.val();//设置查询字符串值
$.ajax({
键入:“获取”,
url:“increment_count.php”,
数据:qs,
错误:函数(xhr、状态、消息){
警报(“未能处理单击计数:“+msg”);
}
})
.done(函数(){
getTotals();//在成功处理时更新总计
});
}
}
$(文档).ready(函数(){
getTotals();//获取初始页面加载时的单击总数
$(文档).ready(函数(){
//将click incrementing事件处理程序添加到页面上的所有单选按钮
$('input:radio')。单击(函数(){
processClick($(this));
});
});
});
下面是get_count.php
<?php
require('db_connect.php');
// get new count totals, pass as JSON
$rs = mysql_query("SELECT * FROM employee") or die('Cannot get updated click counts');
if(mysql_num_rows($rs) > 0) {
$out = "{ ";
while($row = mysql_fetch_array($rs)) {
$out .= "\"$row[name]\" : $row[leave], ";
}
$out = substr($out, 0, strlen($out) - 2);
$out .= " }";
header("Content-type: application/json");
echo $out;
}
<?php
require('db_connect.php');
// if this is a postback ...
if(isset($_GET['weekend'])) {
// create array of acceptable values
$ok = array('security', 'manager', 'cleaner');
// if we have an acceptable value for position_name ...
if(in_array($_GET['weekend'], $ok)) {
// update the counter for that position
$q = mysql_query("UPDATE employee SET leave = leave + 3 WHERE name = '".$_GET['weekend'] . "'") or die ("Error updating count for " . $_GET['weekend']);
}
}
到底是什么不起作用?您的PHP脚本调用成功吗?我的脚本调用成功。当我使用相同的代码只是增加另一个虚拟表以进行测试时,数据库值还没有增加。它可以工作,但在我的主表上不工作。嗨,看起来应用程序的每个部分都是正确的。您是否从increment_count.php收到die()消息?不,我没有收到die()消息消息,当我使用相同的代码只是为了测试目的增加另一个虚拟表时,它可以工作,但在我的主表上不工作。@maqhawesmaline首先将$q=mysql\u query…
替换为return mysql\u query
,以了解发生了什么。我已经测试了你的代码。如果在查询字符串(“weekend=“+obj.val()
)中发送正确的值,则唯一的问题可能是数据库连接。您确定increment\u count.php
具有db\u conn访问权限吗?