mardi 6 décembre 2016

How do I add a checkbox in to a ajax table so when the checkbox is checked, make it add 1 into my table?

I'm using AJAX POST methods to POST my code that's entered into the database. I want to add in a checkbox so that when a user checks it, (it automatically refreshes) AND, mainly so that it puts the value of 1 in to the database (only if it's checked).

Here is my table code :

<tbody>

<?php
foreach($faq as $k=>$v) {

/* ------------------------------------------------------------------*/

$now = new DateTime();
$clientTime = $faq[$k]["time"];
$future_date = new DateTime($clientTime);

$interval = $future_date->diff($now);


/* ------------------------------------------------------------------*/

?>
<tr class="table-row" style="text-align: left;" id="no_enter">
<td style="border: 1px solid #eee; text-align: center; font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'time','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["time"] != '' ? $interval->format("%a days, %h hrs, %i mins") : 'None'; ?></td>
<td style="border: 1px solid #eee; text-align: center; font-size: 11px;" contenteditable="true" onBlur="saveToDatabase(this,'enabled','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["enabled"]  == 1 ? '<form action="includes/saveedit_members.php" method="POST"><input name="enabled_checkbox" value="1" type="checkbox" checked><input type="submit" value="go!"></form>' : '<form action="includes/saveedit_members.php" method="POST"><input name="enabled_checkbox" value="1" type="checkbox"><input type="submit" value="go!"></form>'; ?></td>
</tr>
<?php
}
?>

</tbody>

Only focus on the enabled column. If you can read the code I'm trying and help me, that would be great.

My AJAX sending code is correct. This is my AJAX PHP receiving page :

<?php
if(!isset($_POST['column']) || !isset($_POST["editval"]) || !isset($_POST["id"])) {
    header('Location: error-pages/index.php');
} else if(isset($_POST['column']) && isset($_POST["editval"]) && isset($_POST["id"])) {
    mysqli_query($con, "UPDATE users set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE  id=".$_POST["id"]);
    if(isset($_POST['enabled_checkbox']) {
        alert('Well done harry');
    } else if(!isset($_POST['enabled_checkbox']) {
        alert('Well done harryz');
    }
}
?>

Here I'm trying to update the value of the table 'enabled' to 1 if the checkbox is checked.

Aucun commentaire:

Enregistrer un commentaire