Checkbox with a unchecked value.

Hi,

sorry, I’m pretty new with PHP but I gotta start somewhere, I’m having a problem processing a simple form to update a database.

I want a checkbox to value ‘1’ if it is checked and a value ‘0’ if it is not. I’ve looked at various forums but unable to find a solution that works for me.

the nearest thing I’ve found is using a hidden file with the same name (closed[]) as the checkbox name, but when the form posts
both values are entered and the array goes out of sync.

here is my form page:-

[php]

<?php $page = "test"; @include("admin_head2.php"); @include("../inc/cfd.php"); $cxn = mysqli_connect($host,$username,$password,$db_name) or die ("couldn't connect to Server"); $query = "SELECT * FROM $tbl_open"; $result = mysqli_query($cxn,$query) or die ("couldn't execute query"); $i = 0; ?>
<? $count=mysqli_num_rows($result); while($rows=mysqli_fetch_array($result)){ ?> <?php ++$i; } ?>
ID Day   Open   Closed   Closed All Day
<? echo $rows['id']; ?>       />
             
<? mysqli_close(); @include("admin_foot.php"); ?>

[/php]

and here is the form process page (update_times.php).

[php]<
?php
@include("…/…/inc/cfd.php");
$cxn1 = mysqli_connect($host,$username,$password,$db_name)
or die (“couldn’t connect to Server”);

$size = count($_POST[‘id’]);
$i = 0;

while ($i<$size)
{
$id = $_POST[‘id’][$i];
$day = $_POST[‘day’][$i];
$start = $_POST[‘start’][$i];
$stop = $_POST[‘stop’][$i];
$closed = $_POST[‘closed’][$i];

$query = “UPDATE opentimes SET day = ‘$day’,
start = ‘$start’,
stop = ‘$stop’,
closed = ‘$closed’
WHERE id = ‘$id’ LIMIT 1”;
mysqli_query($cxn1,$query) or die (“Error in query: $query”);
++$i;
}

mysqli_close();
?>[/php]

I seem to keep going round and round in circles. If anyone can help I would really appreciate it.

Many thanks. :slight_smile:

very easy actually. Since the value can either be 1 or 0, use an if(empty()) statement to see if the checkbox is checked. if it is, then the value is 1, if not, either set the post or the associated variable to 0.

Hi Richei,

Many thanks for your reply.

How and where would I do that?

The process page updates multiple rows and the $_POST[‘closed’] doesn’t recognise the empty checkboxes (if 2 boxes ticked out of 7 - I get an array of only 2 not 7 values).

how do I set the if (empty(() before the form is processed?

Cheers

Sponsor our Newsletter | Privacy Policy | Terms of Service