mardi 17 octobre 2017

Checkbox insert in mysql and check if checked

After i searched for this solution on this site, nothing i found. Here is basic php code, just for testing.

index.php

    <input type="checkbox" name="chk" id="chk" value="1" />
<button type="button" name="" id="submit">TEST</button>
<script src="http://ift.tt/2nYZfvi"></script>
<script>  
 $(document).ready(function(){  
      $('#submit').click(function(){  
           var check = $('#chk').val();  

                $.ajax({  
                     url:"qry.php",  
                     method:"POST",  
                     data: {check:check},  
                     success:function(data)  
                     {  
                          //alert(data);  
                          window.location.reload();

                     }
                });   

      });   
 });  
 </script> 

qry.php

<?php  

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "check";

// Create connection
$connect = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($connect->connect_error) {
    die("Connection failed: " . $connect->connect_error);
} 

if($_REQUEST['chk'] == true){
    $stok = '1';
}
elseif ($_REQUEST['chk'] == false) {
    $stok = '0';
}
      $query = "INSERT INTO test(checked) VALUES('$stok')";  
      $result = mysqli_query($connect, $query);  
        if ($result === TRUE) {
            echo "Zahtev je uspešno poslat!";

        } else {
            echo "Error: " . $query . "<br>" . $connect->error;
        }   

 ?>  

How to set checkbox checked true or false into mysql and then echo if in html? It's always set to 0 in mysql boolen.

<input type="hidden" name="chk" id="chk" value="1" <?php if ($checked == '1') {echo 'checked';} else {} ?>/>

I tried everything from this site and nothing works.




Aucun commentaire:

Enregistrer un commentaire