Send value checkbox into sql database field

I have this table


and I want when to check a checkbox to insert(or update) into the post_check database field with the values checked or unchecked (without submit button) correspondingly of the checkbox . I write this code but I have stuck for a long time because the values didn’t pass to database, the tick mark didn’t stay in checkbox (even I made reload with header() function)`Preformatted

	<?php  
       
       global $connect;
       $query="SELECT * FROM posts ORDER BY post_date DESC ";
       $select_posts= mysqli_query($connect, $query); 
       while($row=  mysqli_fetch_assoc($select_posts)) {
       $post_id=$row['post_id'];
       $post_cat_title=$row['post_cat_title'];
       $post_title=$row['post_title'];
       $post_keimeno=  substr($row['post_keimeno'], 0, 60);
       $post_publisher=$row['post_publisher'];
       $post_image=$row['post_image'];
       $post_date=$row['post_date'];
       $post_check=$row['post_check'];
       
  
       
       
       echo "<tr>";?>
    
    <td><input type='checkbox' name="checkbox" id="<?php echo $post_id;  ?>" value="<?php echo  $value;  ?>" onchange="check(this);"  ></td>;
       
       <?php
       echo "<td class='keli'><strong>$post_id</td>";
       echo "<td class='keli'>$post_cat_title</td>";
       echo "<td class='keli'>$post_title</td>";
       echo "<td class='keli'>$post_keimeno</td>";
       echo "<td><img src='../images/$post_image' height='50' witdth='50'></td>";
       echo "<td class='keli'>$post_publisher</td>";
       echo "<td class='keli'>$post_date</td>";
       ?>
       <td><a href="tables.php?source=tables&update=<?php echo $post_id; ?>">UPDATE</a></td>
       <td><a href="tables.php?source=tables&delete=<?php echo $post_id; ?>">Delete</a></td>
       </tr>
       <?php
        }
        
	?>
                 
    
    </table>
</form>
<div class="button">	
        <!--<button class="koumpi" type="submit" name="submit">SUBMIT</button>-->
        <button type="submit" name="delete"class="btn btn-primary">Delete</button>
        <button type="submit" class="btn btn-primary" name="update" style="background-color: green;">UPDATE</button>
        <button type="submit" class="btn btn-primary">Submit Button</button>
        
    </div>
<script>
        function check(ch){
            if(ch.checked){
                window.location.href = "tables.php?source=tables&checkbox="+ch.id+"&value=checked";
                
                
            }
            else{
                window.location.href = "tables.php?source=tables&checkbox="+ch.id+"&value=unchecked";
              
            }
        }
        
    </script>

Could you please include all of your code? Your are missing an opening <form> tag with the method and action attributes. What does the code that processes your form look like? We really need to see these if you want us to help.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Lumino - Tables</title>

<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/datepicker3.css" rel="stylesheet">
<link href="css/bootstrap-table.css" rel="stylesheet">
<link href="css/styles.css" rel="stylesheet">

<!--Icons-->
<script src="js/lumino.glyphs.js"></script>

<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->

</head>



<body>
    
	            
                    <!--Header-->
        <?php include 'includes/header.php';
              include '../main_includes/connect_db.php'; 
              include 'includes/functions.php';
        ?>
                    <!--end Header-->
		
                    
    <!-- .SIDEBAR -->	
	<?php  include 'includes/sidebar.php';  ?>
    <!--/.sidebar-->
    
		
	<div class="col-sm-9 col-sm-offset-3 col-lg-10 col-lg-offset-2 main">			
		<div class="row">
			<ol class="breadcrumb">
				<li><a href="#"><svg class="glyph stroked home"><use xlink:href="#stroked-home"></use></svg></a></li>
				<li class="active">Icons</li>
			</ol>
		</div><!--/.row-->
		
		<div class="row">
			<div class="col-lg-12">
				<h1 class="page-header">Tables</h1>
			</div>
		</div><!--/.row-->
             </div>        
	
			
<form action="" method="post">
 <table class="table table-bordered table-hover"  id="pinakas">
    <thead>
        <tr>
            <th class="titlos"><input style="margin-left: 3px;" type="checkbox" name="checkboxAllBoxes" id="checkboxAllBoxes "></th>
            <th class="titlos">POST_ID</th>
	    <th class="titlos">ΚΑΤΗΓΟΡΙΑ</th>
	    <th class="titlos">ΤΙΤΛΟΣ</th>				        
            <th class="titlos">ΚΕΙΜΕΝΟ</th>                                            
            <th class="titlos">FILE</th>
            <th class="titlos">ΕΚΔΟΤΗΣ</th>
            <th class="titlos">ΗΜΕΡ.</th>
            <th class="titlos">UPDATE</th>
            <th class="titlos">DELETE</th>
	 </tr>					   
    </thead>					    
				    
		
	<?php  
       
       global $connect;
       $query="SELECT * FROM posts ORDER BY post_date DESC ";
       $select_posts= mysqli_query($connect, $query); 
       while($row=  mysqli_fetch_assoc($select_posts)) {
       $post_id=$row['post_id'];
       $post_cat_title=$row['post_cat_title'];
       $post_title=$row['post_title'];
       $post_keimeno=  substr($row['post_keimeno'], 0, 60);
       $post_publisher=$row['post_publisher'];
       $post_image=$row['post_image'];
       $post_date=$row['post_date'];
       $post_check=$row['post_check'];
       
  
       
       
       echo "<tr>";?>
    
    <td><input type='checkbox' name="checkbox" value="<?php echo $post_check  ?>"></td>;
       
       <?php
       echo "<td class='keli'><strong>$post_id</td>";
       echo "<td class='keli'>$post_cat_title</td>";
       echo "<td class='keli'>$post_title</td>";
       echo "<td class='keli'>$post_keimeno</td>";
       echo "<td><img src='../images/$post_image' height='50' witdth='50'></td>";
       echo "<td class='keli'>$post_publisher</td>";
       echo "<td class='keli'>$post_date</td>";
       ?>
       <td><a href="tables.php?source=tables&update=<?php echo $post_id; ?>">UPDATE</a></td>
       <td><a href="tables.php?source=tables&delete=<?php echo $post_id; ?>">Delete</a></td>
       </tr>
       <?php
        }
        
	?>
                 
    
    </table>
</form>
    
    
    <div class="button">	
        <!--<button class="koumpi" type="submit" name="submit">SUBMIT</button>-->
        <button type="submit" name="delete"class="btn btn-primary">Delete</button>
        <button type="submit" class="btn btn-primary" name="update" style="background-color: green;">UPDATE</button>
        <button type="submit" class="btn btn-primary">Submit Button</button>
        
    </div>
    <script>
        function check(ch){
            if(ch.checked){
                window.location.href = "tables.php?source=tables&checkbox="+ch.id+"&value=checked";
                
                
            }
            else{
                window.location.href = "tables.php?source=tables&checkbox="+ch.id+"&value=unchecked";
              
            }
        }
        
    </script>
<!--/.row-->	
	 <!--/.main-->
              
	<script src="js/jquery-1.11.1.min.js"></script>
	<script src="js/bootstrap.min.js"></script>
	<script src="js/chart.min.js"></script>
	<script src="js/chart-data.js"></script>
	<script src="js/easypiechart.js"></script>
	<script src="js/easypiechart-data.js"></script>
	<script src="js/bootstrap-datepicker.js"></script>
	<script src="js/bootstrap-table.js"></script>

		
	<!--/.main-->

		
</body>

</html>

Well, according to this: <form action="" method="post">, it looks like your form is not being processed at all. That’s why nothing is going to the database.

I tried with various codes and methods, but nothing.I think I shall be be use ajax.But the problem is that i don’t know ajax yet.

My suggestion would be to get it working first, then worry about making it work with Ajax, especially as you don’t have Ajax experience. Add an “action”, create and debug that script, get the basics working.

Of course, you could leave action blank as you have it, and add the form-processing code at the beginning of the same file. I’m not sure whether there’s a hard-and-fast rule as to when this approach is better than having the form processing in a separate script. I’m sort of thinking that if you want to end up with Ajax processing the updates, a separate script might be better.

If you leave it blank, when you press ‘submit’ the form contents will be passed back to the same script - leaving it blank effectively says “reload the current script, and pass the form contents to it”. So you’d need to add code at the start to see whether the form contents are set, and process them if they are.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.