skip to Main Content

I’m new to php and html and i need some help for my project. I have seen a few examples online but i don’t quite understand them. What i want to do is to update the database using a form with a checkbox. If it is checked then the database should be updated as 1. Else if it is unchecked then update it with 0. Currently, my issue is not knowing how to make it so that the code will update the database with 1 or 0 if it is checked or unchecked. I know that i have to create a separate php file to do it but i’m not sure how to do it. Below is the code i used for the form. Please help me.

<i class="icon-book text-muted"></i> &nbsp; Select modules to enroll student in:<br><br>
                                        <div class="form-group has-feedback has-feedback-left" style="padding-left:25px">       
                                            <div class="checkbox">
                                                <input type="checkbox" name="module[1]" value="1"><label>MRO</label>
                                            </div>      
                                    </div>
                                    
                                    <div class="form-group has-feedback has-feedback-left" style="padding-left:25px">   
                                            <div class="checkbox">
                                                <input type="checkbox" name="module[2]" value="1"><label>CNC</label>
                                            </div>      
                                    </div>
                                    
                                    <div class="form-group has-feedback has-feedback-left" style="padding-left:25px">   
                                            <div class="checkbox">
                                                <input type="checkbox" name="module[3]" value="1"><label>Aero-Science</label>
                                            </div>      
                                    </div>
                                    
                                    <div class="form-group has-feedback has-feedback-left" style="padding-left:25px">   
                                            <div class="checkbox">
                                                <input type="checkbox" name="module[4]" value="1"><label>Aero-Structures</label>
                                            </div>      
                                    </div>
                                    
                                    <div class="form-group has-feedback has-feedback-left" style="padding-left:25px">   
                                            <div class="checkbox">
                                                <input type="checkbox" name="module[5]" value="1"><label>Aero-Systems</label>
                                            </div>      
                                    </div>

UPDATE: Now that i am using the insert function. If i select only one of the checkbox, the selected checkbox gets updated in the database. However, i will get an error code of, Undefined index: module_2, if i did not select the second checkbox. In the table in the database, it the unchecked modules appear as empty and not 0. How do I make it so that the unchecked boxes will appear as 0 in the database. I’m using phpMyadmin as the database btw.

3

Answers


  1. First, you need to enclose your code in a form. Like this,

    <form action='submit_form.php' method='POST'>
    <div class="form-group has-feedback has-feedback-left" style="padding-left:25px">   
         <div class="checkbox">
              <input type="checkbox" name="module_2" value="1" checked><label>CNC</label>
         </div>      
    </div>
    .....
    .....
    <input type="submit" value="Submit">
    </form>
    

    Then in your submit_form.php file get the value like this,

    $module_2= $_POST['module_2'];
    

    Then using mysql insert query, push your values to db table.

    Login or Signup to reply.
  2. OK for that you have to send AJAX request. So that you can easily update database without refreshing the current page. You can handle it like this-

    $('#module_one').change(function(e) {
      e.preventDefault();
      let module_one_val = $(this).val();
      // now send ajax request to update databse
      $.get("url", {
              'module_one': module_one_val
          },
          function(data, textStatus, jqXHR) {
              console.log(data);
              alert('database updated');
          }
       );
     });
    

    Note : don’t forget to set id name in input field & jquery CDN.

    Login or Signup to reply.
  3. Hi You can use isset to assign the 0 or the 1

    if(isset($_POST[module[2]])){
    
    $checkbox = '1';
    }else{
    
    $checkbox = '0';
    
    }
    

    Then use these values in your PDO or your prepared statement and it will update it

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search