2013-10-28 90 views
0

當我點擊一個複選框時,我試圖將數據庫中的一個值從0更新爲1。我已經通過了很多關於這個主題的例子,但我似乎不明白我做錯了什麼。我相當新的這一點,並會真正感謝一些幫助。當複選框被選中時用AJAX更新數據庫

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

?> 
+1

您的帖子變量是'id' /關閉否則只能觸發,但在PHP,你想用它作爲' $ _POST ['application_id']',這就是你的問題! –

回答

1

$_POST["application_id"]總是要爲空,因爲你正在過字段爲$_POST["id"]從您的AJAX。

無論是在你的PHP更改此:

$application_id = $_POST["id"]; 

或這在你的JavaScript:

data: {application_id: application_id} 
1

請使用以下的變化,

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

checkCheckboxState功能刪除下面提及的條件,這樣它可以作爲切換像時複選框得到遏制

if ($('.approveME').is(':checked')) { 
} 
相關問題