Keeping checkboxes checked

Hi, I have a function which displays several rows from my database. The results are then returned in the value of several checkboxes in a form. When the form has an error (i.e. a text input is empty), and a checkbox was previously checked will be unchecked after the error.

This is my function

function displaylisting($row) {

global $generic;

if(empty($row)) return false;

 $list = $_GET['list'];
$stmt      = $generic->query("SELECT * FROM `xxx` WHERE `token` = '$list'");
			$user_row = $stmt->fetch(PDO::FETCH_ASSOC);
			
	
			
				$startDate = $row['start']; 
$endDate = $row['end']; 
$freq = $row['freq']; 

$startTime = strtotime($startDate);
$endTime = strtotime($endDate);
$currentDateUnix = $startTime;
$weekNumbers = array();
while ($currentDateUnix < $endTime) {
    $firstDate = date('dS M Y', $currentDateUnix);
    $currentDateUnix = strtotime('+7 days', $currentDateUnix);
    $strEnd = strtotime('-1 days', $currentDateUnix );
    $endDate = date('dS M Y', $strEnd);
  array_push($weekNumbers, array("start" => $firstDate, "end" => $endDate));
    }
foreach($weekNumbers as $key => $result) {

?>

<div class="section-wrapper"><div class="date-container">
 <span class="buy-view"><?php echo $result['start']; ?></span>
 <span class="list-divider">-</span>
 <span class="buy-view"><?php echo $result['end']; ?></span>
</div>
<div class="ck-button">
  <label>
  <input type="checkbox" value="<?php echo $result['start']; ?>/<?php echo $result['end']; ?>" name="dateSelect[]"><span>Select</span>
  </label>
 </div>  
 </div>
    <?php
 }  

Iā€™ve been trying to use something like this -

<?php if (isset($_POST['dateSelect']) == $result['start'].'/'.$result['end']) { ?> checked="checked" <?php } ?>

But all the checkboxes become checked. Can anyone help please?

In that code, youā€™re comparing the return from the isset() function (which will be a Boolean true or false value) to a pair of strings concatenated with a central / character, so thatā€™s never going to work. But, the section where you output ā€˜checked=ā€œcheckedā€ā€™ is outside the PHP code section, so will output regardless of whether your if() comparison is correct or not. Once you have the if() working, change that part to be echoed from within the PHP code. I think.

2 Likes

Yeah, right, but if you use the some alternative syntax for control structures you can.

In your case, that should be that:

<?php if (isset($_POST['dateSelect']) == $result['start'].'/'.$result['end']): ?>
checked="checked"
<?php endif; ?>

Note: I didnā€™t verified if your control is really right.

1 Like

Thanks for the advice guys, still really struggling here. I cannot seem to get it working rightā€¦

Iā€™ve just read the @droopsnoot answer. So yeah, your checking is wrong.

Try this:

 <?php if (isset($_POST['dateSelect']) && $_POST['dateSelect'] == $result['start'].'/'.$result['end']): ?>
checked="checked"
<?php endif; ?>

Before that was like:

if (boolean == string)

isset() function return a boolean.

1 Like

Thank you @Alaanor but it still isnā€™t working. Non of them remain checked

Can you say me what return:

$result['start'].'/'.$result['end']`

and

`$_POST['dateSelect']`.

Several checkboxes can be checked at one time, however Iā€™ve just checked one in the below example and here are the results:

$result[ā€˜startā€™].ā€˜/ā€™.$result[ā€˜endā€™]` = 14th Oct 2015/20th Oct 2015

$_POST['dateSelect']. = 14th Oct 2015/20th Oct 2015

This is how Iā€™m using your code -

foreach($weekNumbers as $key => $result) {
?>

<div class="section-wrapper"><div class="date-container">  
<span class="buy-view"><?php echo $result['start']; ?></span> 
<span class="list-divider">-</span>
<span class="buy-view"><?php echo $result['end']; ?></span>
</div> 
<div class="ck-button">
<label>
  <input type="checkbox" value="<?php echo $result['start']; ?>/<?php echo $result['end']; ?>" name="dateSelect[]"  <?php if (isset($_POST['dateSelect']) && $_POST['dateSelect'] == $result['start'].'/'.$result['end']): ?>
checked="checked"
<?php endif; ?>><span>Select</span>
 </label>
</div>
</div>
<?php
 }

And what happening if you try the normal syntax:

<?php if (isset($_POST['dateSelect']) && $_POST['dateSelect'] == $result['start'].'/'.$result['end']){ echo 'checked="checked"' } ?>

None remain checked, still

Very strange, I canā€™t locate where is the error. Did you verified if the generated source code doesnā€™t have any typo error ?

Yes, itā€™s very strange. Iā€™ve looked over the generated code and it looks fine and everything is working great. Only problem its not remaining checked after submitā€¦

Thank you for all your help anyway @Alaanor.

Ok so Iā€™ve been playing around with your code Alaanor, and managed to get it working. Here is the working code -

if(isset($_POST['dateSelect']) && in_array($result['start'].'/'.$result['end'], $_POST['dateSelect'])) { $select = "checked='checked'"; } 
else { $select = ''; }

Then I simply echo $select; in the checkbox.

Thanks for your help

Haaaaa ! $_POST['dateSelect'] is an array ! Iā€™ve didnā€™t noticed this, sorry :sweat_smile:

1 Like

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