Jump to content

array_diff to insert all unchecked checkbox


ilovephp

Recommended Posts

is it possible to make INSERT all checkbox values into database when there is not even one checkbox being checked since im using array_diff as shown below?

 

my problem currently facing is,

when there is a checkbox being checked, it will insert into checked_table and the remainder options which did not selected will insert into unchecked_table.howewer, when all checkbox didnt being selected, and click on submit button, the value didnt send to my database unchecked_table.

<?php	include('connect.php');	$checked =  ($_POST['date']);	$unchecked = ($_POST['nondate']);	$diff = array_diff($unchecked,$checked);	            if(isset($_POST['date']))            {                foreach ($_POST['date'] as $dateValue)                {                $insert="INSERT INTO checked_table ($colm_ladate) VALUES ('$dateValue')";		mysql_query($insert);                }		echo header("location:home.php");		}		if (isset($_POST['nondate']))		{		foreach ($diff as $dateValue2)		{	$insert2="INSERT INTO unchecked_table($colm_lrdate) VALUES ('$dateValue2')";	mysql_query($insert2);		}	echo header("location:home.php");            }?>
Link to comment
Share on other sites

it did redirect to the home.php but without insert all the unchecked check box value into database.

 

i got no idea how to make user did not select any of the checkbox and click submit and it will insert all the unchecked check box value into the unchecked table in the database with the code i write above.

Link to comment
Share on other sites

It seems that it is entering the first if statement then. Is $_POST['date'] a HTML checkbox as well? $_POST['date'] for some reason is considered 'set' when being checked in the isset() function. Typically if the checkbox is 'checked', it is considered 'set' for the isset() function and NOT set if it's NOT checked.

 

If its not a checkbox but a input of type "text," regardless if there is a value to it or not, it will be considered 'set' to the isset() function.

 

If possible, posting the HTML form that goes with this code would be great.

Link to comment
Share on other sites

Hi, thanks for your reply.Here is my HTML form coding.

<form id="date_status" name="date_status" method="post" action=""><label>Date:- <b>FROM</b></label><input type="date" name="ldatefrom" value="<?php echo $datefrom;?>" disabled><label><b>TO</b></label> <input type="date" name="ldateto" value="<?php echo $dateto;?>" disabled><br>	<?php$startDate = strtotime($datefrom);  $endDate = strtotime($dateto);  $date = $startDate;  	while( $date <= $endDate) {		echo  "<input type='hidden' name='nondate[]' value ='".date('Y-m-d', $date)."'>";	echo  "<input type='checkbox' name='date[]' value ='".date('Y-m-d', $date)."'>";	echo date('Y-m-d', $date) .' ';  	$date = strtotime('+1 day', $date); 	}?>	<br/><input id="submit_hod" type="submit" name="submit"> <br> 	</form>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...