0

Actually I want to submit the value of both checked and unchecked check boxes at same time using php. Will any one can help me?

This is the code which I am using now just for example so you can become more close to my problem.

In php code in first for loop I am submitting the checked values and then after that I fetch all the values from the database which are not in checked array and then I insert these values again into database in second insert query

Is there be any solution with which instead of using two insert queries I can do same thing with single query.

<!DOCTYPE html>
<html>
<body>
<?php
if (isset($_POST['submit'])) {
 for ($i=0; $i < count($_REQUEST['marked']) ; $i++) {
  $rollno=$_REQUEST['marked'][$i];
  $check=1;
  $result=$dbh->prepare("INSERT INTO attendencerecord (subjectcode, coursename, rollno,markedon, present,session,semester)  VALUES('$subjectcode','$coursename','$rollno','$date','$check','$session','$semester')");
  if($result->execute()){
   echo 'success';
  }
  else{
   echo 'fail';
  }
 }
 $absents=array();
 $absents=$_REQUEST['marked'];
 $query="SELECT rollno FROM studentslist WHERE session = '$session' AND rollno NOT IN(".implode(',',array_map('intval',$absents)).")";
 $result=mysqli_query($connection,$query);
 while($ro=mysqli_fetch_array($result)){
  $ro['rollno'];
  $check=0;
  $result1=$dbh->prepare("INSERT INTO attendencerecord (subjectcode, coursename, rollno,markedon, present,session,semester)  VALUES('$subjectcode','$coursename','$ro[rollno]','$date','$check','$session','$semester')");
  if($result->execute()){
   echo 'success';
  }
  else{
   echo 'fail';
  }
 }
}
?>
<form method="post">
    <input type="checkbox" name="marked[]" id="c1"  value="2901" checked />
    <label for="c1">2901</label>
    <input type="checkbox" name="marked[]" id="c2" value="2902" checked />
    <label for="c2">2902</label>
    <input type="checkbox" name="marked[]" id="c3" value="2903" checked />
    <label for="c3">2903</label>
    <input type="checkbox" name="marked[]" id="c4" value="2904" checked />
    <label for="c4">2904</label>
    <input type="checkbox" name="marked[]" id="c5" value="2905" checked />
    <label for="c5">2905</label>
    <button type="submit" name="submit">submit</button>
</form>

  • If you know the values of the checked boxes, then you by default, also know what boxes are not checked. – Ryan Kozak Jul 12 '18 at 05:07
  • 1
    This question asked by someone already. please read this [link](https://stackoverflow.com/questions/19239536/how-get-value-for-unchecked-checkbox-in-checkbox-elements-when-form-posted) – Evince Development Jul 12 '18 at 05:11
  • My Question is about How I can post values of both checked and unchecked check boxes into data base at same time?I need help in code. – parmpreet singh Jul 12 '18 at 05:12
  • Now I first insert the values of all checked boxes then fetch values of all unchecked boxes using NOT IN .impload method in query and then I insert these values again into database.This process takes time and is very lengthy.So I need some simple method to post all these values at same time – parmpreet singh Jul 12 '18 at 05:18
  • Now I attached a code of my problem I hope now you can easily understand my problem – parmpreet singh Jul 12 '18 at 06:05

0 Answers0