This is my PHP code:
I have to make when I check or uncheck a checkbox, then immediately update a value into the database.
I need to do this with PHP or Javascript.
<?php
session_start();
include("head.php");
include("dbconnection.php"); // database connectie
if (isset($_SESSION['login']))
{
echo "<h1>Contacten</h1>";
echo "<h3>Welkom ".$_SESSION['login']."</h3><br/>";
include("menu.php");
$sql = "SELECT * FROM `customers` ORDER BY Created DESC "; // selecteer uit tabel customers
echo "<table class='overzicht'>
<tr style='background-color: lightgray; min-height: 200px;' >
<td colspan='9'>
<a href='#' id='module-tab-1' class='toggle' data-prod-cus='1'>
<h1 style='margin-bottom: 0px;'>Alle Contacten</h1>
<div class='left-image hi'></div>
</a>
</td>
</tr>
<tr style='background-color: orange; display:none;' class='cus1' >
<th>Allemaal</th>
<th>Datum</th>
<th>Naam</th>
<th>Bedrijf</th>
<th>Email:</th>
<th>SMS</th>
<th>Storing</th>
</tr>";
foreach ($conn->query($sql) as $row) {
echo "
<tr class='cus1' style='display:none'>";
if ($row['All'] == 1)
{
echo "<td>
<input type='checkbox' name='all' checked>
</td>";
}
else
{
echo "<td>
<input type='checkbox' name='all'>
</td>";
}
echo "
<td>
".$row['Created']."
</td>
<td>
".$row['Name']."
</td>
<td>
".$row['Company']."
</td>
<td>
".$row['Email']."
</td>
<td>
".$row['SMS']."
</td>
<td>
".$row['StoringsID']."
</td>";
echo "</tr>";
}
echo '</table>';
}
else
{
header("location:index.php"); // ga terug naar het inlogscherm
}
include("Javascripts.php");
Can someone tell me how to do this?
I don't know how to do this.
I saw some examples, but they didn't work out for me.
I used Jquery but I don't know how to update the database then.
You can save data by using ajax request
$.ajax({
url: 'save.php',
type: 'POST',
data: 'id='+23,
success: function(data, textStatus, jqXHR){
$("#message-box").text("Successfully!");
},
error: function(jqXHR, textStatus, errorThrown){
var errResponse = JSON.parse(jqXHR.responseText);
},
});
Get checkbox event
<input type='checkbox' name='all' class="mycheckbox" checked>
if($('.mycheckbox').prop('checked')) {
// do something when checked
} else {
// do something else when not
}
The easiest way would be a simple form that is submitted on change: (Replace thispageurl.php with your url)
<form action="thispageurl.php" method="post">
<input id="yourinputid" name="input">
</form>
Now you can add the following js, It submits the form on change:
window.onload=function(){
console.log("load..."); changer=document.getElementById("yourinputid");
console.log(changer);
changer.onchange=function(){
console.log("changed");
//you could also use komals code here ( it does not reload the page)
changer.parentNode.submit();
};
};
Now you need the php (in thispageurl.php or save.php if you use komals code) to catch this:
<?php
if(isset($_POST["input"])){
$newvalue=$_POST["input"];
//update your mysql
}
?>
A small note: Instead of echoing you can simply write the html into the php. Thats easier to read:
<?php if($value==true){ ?>
Value is true
<?php } ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.