luke13
October 24, 2015, 4:27pm
1
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
luke13
October 24, 2015, 9:13pm
4
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
luke13
October 24, 2015, 9:53pm
6
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']`.
luke13
October 24, 2015, 9:58pm
8
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
}
Alaanor
October 24, 2015, 10:09pm
9
And what happening if you try the normal syntax:
<?php if (isset($_POST['dateSelect']) && $_POST['dateSelect'] == $result['start'].'/'.$result['end']){ echo 'checked="checked"' } ?>
luke13
October 24, 2015, 10:12pm
10
None remain checked, still
Alaanor
October 24, 2015, 10:18pm
11
Very strange, I canāt locate where is the error. Did you verified if the generated source code doesnāt have any typo error ?
luke13
October 24, 2015, 10:28pm
12
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 .
luke13
October 24, 2015, 10:41pm
13
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
Alaanor
October 24, 2015, 10:45pm
14
Haaaaa ! $_POST['dateSelect']
is an array ! Iāve didnāt noticed this, sorry
1 Like
system
Closed
January 24, 2016, 5:45am
15
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.