samedi 2 juin 2018

Update database with checkbox values retrieved from database

I want to update database table regarding to checkbox state or values. Here below I retrieve the checkbox states from database.

When user access this page sees the current states of checkboxes.

HTML

<form name="financial_checks" method="post" action="cbchange.php" />
   <input name="provision" type="checkbox" <?php if ($podata_provision == '1') echo "checked='checked'"; ?>/>
   <input name="invoice" type="checkbox" <?php if ($podata_invoice == '1') echo "checked='checked'"; ?>/>
   <input name="payment" type="checkbox" <?php if ($podata_payment == '1') echo "checked='checked'"; ?>/>
   <button type="submit" name="updatebtn" id="btn" class="btn btn-info">Update</button>
</form>

I want to update table without refreshing while user change the state of checkbox, so I'm using Ajax to POST data;

    $.ajax( {
        type: 'POST',
        url: 'cbchange.php',
        data: { provision:provision, invoice:invoice, payment:payment, sendtoken:sendtoken2},


    } );

(sendtoken is a variable retrieved in HTML page, also I can't post)

cbchange.php

$provision = $_POST['provision'];
$invoice = $_POST['invoice'];
$payment = $_POST['payment'];
$sendtoken = $_POST['sendtoken2'];


$query = " UPDATE podata SET provision = '".$provision."', invoice = '".$invoice."',payment = '".$payment."' WHERE token2 = '".$sendtoken."'";
$result = mysqli_query($connect, $query);

When I var_dump($_POST) result is related with the state of checkbox checked or unchecked.

array (size=3)
'provision' => string 'on' (length=2)
'invoice' => string 'on' (length=2)
'updatebtn' => string '' (length=0)

How can I post data as 1 or 0 when user change the checkbox state?




Aucun commentaire:

Enregistrer un commentaire