0

I'm trying to update one value in my database from 0 to 1, when a checkbox is clicked. I've gone through alot of examples on this subject, but i cant seem to understand what i'm doing wrong. Im fairly new to this, and would really appreciate some help.

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
  • 1
    your post variable is id but in php you want to use it as $_POST['application_id'] , thats your problem ! Commented Oct 28, 2013 at 15:25

2 Answers 2

1

$_POST["application_id"] is always going to be null since you are passing the field as $_POST["id"] from your AJAX.

Either change this in your PHP:

$application_id = $_POST["id"];

or this in your Javascript:

data: {application_id: application_id}
Sign up to request clarification or add additional context in comments.

Comments

1

Please use below changes,

In approve.php file, change the
$application_id = $_POST['application_id']; into $application_id = $_POST['id'];

checkCheckboxState function remove below mentioned condition so that it works as toggle like on/off else only trigger when checkbox get checked

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.