Double up
3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 22, 2014 (RSS)
By Toledoh - January 20, 2014
Hi Guys,
Why am I getting a duplicate of the first item I select here?
http://shalomcollege.com/paf/test.php
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php include ("../_globalViewerCode.php") ?>
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="A">
<input type="checkbox" name="check_list[]" value="B">
<input type="checkbox" name="check_list[]" value="C">
<input type="checkbox" name="check_list[]" value="D">
<input type="checkbox" name="check_list[]" value="E">
<input type="submit" />
</form>
<?php
if(!empty($_REQUEST['check_list'])) {
$list = "";
foreach($_REQUEST['check_list'] as $check) {
$list .= "\t".mysql_escape($check)."\t";
echo $list; //Should list all values checked...
}
}
?>
Tim (toledoh.com.au)
By Dave - January 22, 2014
Hey Tim,
That's a tricky bug. If you add a <br/> you'll get this:
echo $list . "<br/>"; //Should list all values checked...
And this output:
A
A B
A B C
A B C D
A B C D E
Because it's outputting the value as it's incrementally building it up. So the solution is to print the value _after_ the loop when it's complete:
<?php
if(!empty($_REQUEST['check_list'])) {
$list = "";
foreach($_REQUEST['check_list'] as $check) {
$list .= "\t".mysql_escape($check)."\t";
}
echo $list; //Should list all values checked...
}
?>
Hope that helps!
interactivetools.com
By Toledoh - January 22, 2014
Ahhh - that makes perfect sense - thanks!
Tim (toledoh.com.au)