1

I'm wanting to make my code so that when I click in the checkbox to enable it, it updates a row in my database to the value 1. My checkbox is inside of my table in php and is coded around AJAX. My issue is that when I click the checkbox, 1) it doesn't do anything, (not even refresh the page using my ajax success status) : `

success: function(data) {
   window.location.replace("admin_members.php");
}

2) It doesn't update my database information giving my column named 'enabled' a value of 1 for when it's checked from the PHP receiver.

My Code is Below :

Html / PHP Table (checkbox is included in here)

<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 ? '<input id="enabled-checkbox" value="1" type="checkbox" checked >' : '<input id="enabled-checkbox" value="1" type="checkbox" >'; ?></td>

AJAX Sender

        function showEdit(editableObj) {
            $(editableObj).css("background","#FFF");
        } 

        function saveToDatabase(editableObj,column,id) {
            $(editableObj).css("background","#FFF url('images/spin.gif') no-repeat right");
            $.ajax({
                url: "includes/saveedit_members.php",
                type: "POST",
                data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
                success: function(data){
                    window.location.replace("admin_members.php");
                }        
           });
        }

PHP Receiver

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"]);
}

A few picture examples to help understand my database and why I'm trying to do :

I want it so if the checkbox is checked, automatically refresh page after updating database column 'enabled' to 1

  • I don't see a database query where you are updating a table named `enabled` – Maximus2012 Dec 06 '16 at 19:49
  • @Maximus2012 oh yes. I have now updated it :) – BenZa Music Dec 06 '16 at 22:06
  • Are you sure the control is going to your AJAX PHP page ? According to your logic, it should either set the value to 1 or 0 so at least one of the queries must be getting executed. – Maximus2012 Dec 06 '16 at 22:43
  • @Maximus2012 i don't think it is going to my AJAX page becuase I even tried just showing a simple alert('Hello'); and it didnt't output anything. The error I think is https://gyazo.com/f5867784f2e1ce903b469004ff5b012c that you have to click in / on the value of the white space where the data is then click out of it to make it update. When i check a checkbox inside there, it just stays checked without refreshing the page because i didn't click in the white space. If you understand ? – BenZa Music Dec 06 '16 at 22:53
  • I think you should update your question with the code for your AJAX request. Are you using jQuery ? You should also use the SO image upload option to make that image a part of your original question to make it more visible. – Maximus2012 Dec 06 '16 at 22:58
  • @Maximus2012 i have now added my ajax code that sends the commands. – BenZa Music Dec 06 '16 at 23:01
  • I think you need to bind the AJAX code to the checkbox checked event. Not sure if your code is doing that. Something like this maybe: http://stackoverflow.com/questions/21164884/ajax-post-and-get-checkbox-value http://stackoverflow.com/questions/13365311/ajax-checkboxes-with-jquery-and-php – Maximus2012 Dec 06 '16 at 23:04
  • @Maximus2012 Although this one is helpful http://stackoverflow.com/questions/21164884/ajax-post-and-get-checkbox-value . It doesn't fix my problem. I need it to update the database as soon as I check the checkbox too. – BenZa Music Dec 06 '16 at 23:32
  • If you do it right then the AJAX/PHP code will take care of the database update. Looks like your problem is that the request is not even going to the PHP code. You might need to fix that first. Also, looking at the image that you posted and also your code, you might need an array of checkboxes with the checkbox id/value containing the information about the row that you want to update in the database. – Maximus2012 Dec 07 '16 at 14:22
  • you have the bind the savetodatabase() function to the checkbox cheched/unchecked event, not sure if you are doing that. – Maximus2012 Dec 07 '16 at 14:34
  • @Maximus2012 I'm now trying to do this : `':''; ?>` but it now does not update or refresh the page. It does nothing. – BenZa Music Dec 07 '16 at 17:24
  • I think it's probably better if you update your question with this additional information. It would make it easier for the users here to assist you. – Maximus2012 Dec 07 '16 at 17:26
  • I am not sure if using onblur function is the best way to go or if it is even working. You might want to bind the checkbox to a checked event via jQuery/AJAX like this: http://stackoverflow.com/questions/6017558/checkbox-checked-or-unchecked-with-jquery-and-mysql http://stackoverflow.com/questions/8423217/jquery-checkbox-checked-state-changed-event – Maximus2012 Dec 07 '16 at 17:30
  • @Maximus2012 I have now updated it, is that more helpful ? – BenZa Music Dec 07 '16 at 17:53
  • Yes but i don't think that's where your problem is. You might want to look at the SO questions/answers that I linked to in my previous comment. – Maximus2012 Dec 07 '16 at 17:56
  • @Maximus2012 I have. None of them have helped me understand. It works without a checkbox becuase of the `data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,` inner html part. A checkbox doesn't have html inner text. Just a value. – BenZa Music Dec 07 '16 at 17:58
  • Are you sure that your AJAX request is getting through ? Are you able to pass the data to the PHP code ? – Maximus2012 Dec 07 '16 at 19:15
  • @Maximus2012 without the checkbox, yes. If I take out the checkbox, it updates my database with the value I type in for example, 1. Using this : `mysqli_query($con, "UPDATE users set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE id=".$_POST["id"]);` in the PHP receive – BenZa Music Dec 07 '16 at 19:21
  • So you need to make sure that on the checkbox checked/unchecked event, there is an AJAX request being sent to the PHP code, which is what some of those questions are about. – Maximus2012 Dec 07 '16 at 19:23
  • @Maximus2012 can you not help me directly? According to my code. – BenZa Music Dec 08 '16 at 17:38

1 Answers1

0

I have done it now :) After hard work.