使用ajax请求更新数据库值

时间:2015-09-10 07:18:06

标签: javascript php jquery mysql ajax

我有一个表单,它有radio个按钮,用户必须点击这些按钮才能在数据库中包含一个值,但我单击radio按钮,数据库中没有任何内容发生在我的表单代码中:< / p>

<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>

这里是表格的脚本

<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>

这里是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;
}

这里是increment_count.php

<?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']);
    }
}

employee表中的休假值未增加

0 个答案:

没有答案