3

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.

1
  • You should use Ajax with PHP to perform the task! Commented Dec 21, 2016 at 11:42

2 Answers 2

1

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 } ?>
Sign up to request clarification or add additional context in comments.

4 Comments

Could u put this into a jsfiddle? It doesn't work for me!
you cant put php into a fiddle... doesnt work doesnt mean that you have to downvote directly
May look into the console, analyze what may not work, i cant help you with that until ive got a specific error...
Could you try the change? Ive added a few logs to detect the mistake
1

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
}

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.