Submit Checkbox values in Form with javascript

This post explains how to submit Check box values in form. Example mail-box selected mails deletion. I had experimented with javascript support.


In this script Checkbox values store in hidden field name store_del. In demo it was a text field.

JavaScript Code

<script type="text/javascript">
function checkAll()
{
  var check_del_len = document.forms[1].check_del.length;
  document.getElementById('st_val').value='';
  for ( i = 0; i<check_del_len ; i++)
  { 
    document.forms[1].check_del[i].checked=true;
    document.getElementById('st_val').value+=document.forms[1].check_del[i].value+',';
  }
  fin_str =  document.getElementById('st_val').value;
  document.getElementById('st_val').value  = fin_str.replace(',on,','');
}

function clearAll()
{
  var check_del_len = document.forms[1].check_del.length;
  for ( i = 0; i<check_del_len ; i++)
  {
    document.forms[1].check_del[i].checked=false;
    document.getElementById('st_val').value='';
  }
}

function test()
{
  var check_del_len = document.forms[1].check_del.length;
  if(check_del_len==0)
  {
    document.getElementById('st_val').value ='';
  }
  document.getElementById('ch_all').checked =false;
  document.getElementById('st_val').value ='';

  for ( i = 0; i<check_del_len ; i++)
  {
    if(document.forms[1].check_del[i].checked)
    {
      document.getElementById('st_val').value+=document.forms[1].check_del[i].value+',';
    }
  }
  fin_str =  document.getElementById('st_val').value;
  document.getElementById('st_val').value  = fin_str.replace(',on,','');
}

function validate()
{
  if(document.getElementById('st_val').value=='')
  {
    alert('Select Tutorials');
    return false;
  }
  else
  {
    return true;
  }
}
</script>

db.php
Database configuration file, modify username, password and database values.

<?php 
define('DB_SERVER', 'localhost'); 
define('DB_USERNAME', 'username'); 
define('DB_PASSWORD', 'password'); 
define('DB_DATABASE', 'database'); 
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

Html/PHP code.
Here you have to modify SQL Statement.

<form action="submit.php" onSubmit="return validate()" method="post">
<input type="hidden" id="st_val" name="store_del">
<input type="submit" value=" Submit " >
</form>
<form>
<table><tr><td>
<input type='checkbox' id="ch_all" name='checkbox' onClick="if(this.checked)checkAll(); else clearAll()" />
</td>
<td><b>User Name</b></td>
</tr>
<?php
$sql=mysql_query("select * from users");
while($row=mysql_fetch_array($sql))
{
  $id=$row['user_id'];
  $name=$row['first_name']. ' '. $row['last_name'];
?>
<tr><td>
<input type='checkbox' name='check_del' onclick='test()' value='<?php echo $id; ?>' />
</td><td> <?php echo $name; ?> </td></tr>
<?php } ?>
</table></form>

Explode! Comma(,) php script
Watch demo ‘store_del’ value split Comma with below PHP fucntion

<?php
if(isset($_POST))
{
  $st_val = $_POST['store_del'];
  $store_del_exp = explode("," ,$st_val);
  $store_del_exp_count=count($store_del_exp); for($j=0; $j<$store_del_exp_count;$j++)
  {
    $t_id = trim($store_del_exp[$j]);
    echo $t_id;
  }
}

?>

 

Please follow and like us:

Add a Comment