选中复选框时,使用AJAX更新DB

时间:2013-10-28 15:16:14

标签: php jquery ajax database

当我点击一个复选框时,我正在尝试将数据库中的一个值从0更新为1。我在这个问题上经历了很多例子,但我似乎无法理解我做错了什么。我是相当新的,非常感谢一些帮助。

FORM:

            <form method='post'>
                <table>                             
                    <thead>                           
                    <th><div style="border-bottom: 1px solid #FFF; width:746px;"><div class="headline1">Registrations under preparation</div></div></th>
                    </thead>
                </table>
                <table class="width_70">   
                    <thead>                           
                    <th class="headline1">Student</th>
                    <th class="headline1">Company</th>
                    <th class="headline1">Date</th>
                    <th class="headline1">P</th>
                    <th class="headline1">V</th>
                    <th class="headline1">K</th>
                    </thead>
                   <?foreach($data_array_praktikant_info as $value){if(($value[1]->studentApproved == 1) xor ($value[1]->companyApproved == 1)) {
                    ?><tr><?                         
                    ?><td><? echo $value[0]->f_name . ' ' . $value[0]->l_name;?></td>
                        <td>Dania</td>
                        <td><? echo $value[1]->approvedDate; ?></td>
                        <td><div class="approve"><input disabled  <? if($value[1]->studentApproved == 1){?>checked<?} ?> id="7" type="checkbox"><label for="7"><span></span></label></div></td>
                        <td><div class="approve"><input disabled  <? if($value[1]->companyApproved == 1){?>checked<?} ?>  id="8" type="checkbox"><label for="8"><span></span></label></div></td>
                        <td><div class="approve"><input class="approveME" value="<? $value->application_id ?>" onclick="checkCheckboxState();" <? if($value[1]->koordinatorApproved == 1){?>checked<?} }}?> id="9" type="checkbox"><label for="9"><span></span></label></div></td>
                    </tr>
                </table>
            </form>

AJAX:

function checkCheckboxState() {

    if ($('.approveME').is(':checked')) {
        var application_id = $('.approveME').first().attr( "value" );

        //tried this aswell// 

        var application_id = $('.approveME').val();

        $.ajax({
            type: "POST",
            url: "approve.php",
            data: {id : application_id},
            success: function(msg) {
                alert("Data saved:" + msg);
            }
        });
    }
    ;
}

PHP :( approve.php)

 <?

 global $wpdb;

 $application_id = $_POST['application_id'];

 $date_current = Date('Y-m-d H:i:s');

 $fieldarray = array('koordinatorApproved' => 1, 'updated' => $date_current);

 $where = array('application_id' => $application_id);

 $wpdb->update('application', $fieldarray, $where);

 ?>

2 个答案:

答案 0 :(得分:1)

$_POST["application_id"]始终为null,因为您从AJAX中将字段作为$_POST["id"]传递。

在PHP中更改此内容:

$application_id = $_POST["id"];
在您的Javascript中

或者这个:

data: {application_id: application_id}

答案 1 :(得分:1)

请使用以下更改

在approve.php文件中,更改
       $ application_id = $ _POST ['application_id'];进入$ application_id = $ _POST ['id'];

checkCheckboxState函数删除下面提到的条件,以便它像开/关一样工作,否则仅在选中复选框时触发

if ($('.approveME').is(':checked')) {
}