wkilc Posted August 20, 2010 Share Posted August 20, 2010 Hello all, The following script is used to query an array. (www.example.com/test.php?lev[]=MS&lev[]=College) Currently, it echos the values queried and simply lists them (echo "$lev (br />\n"). Is it possible to echo those values by keeping the checkbox in question checked instead? <?php echo "<b>Levels <br /> />"; if(!empty($_GET['lev'])) foreach($_GET['lev'] as $lev){ echo "$lev <br />\n"; } ?> <form /> <input type="checkbox" name="lev[]" value="PreK" /><font class="sm">PreK</font> <input type="checkbox" name="lev[]" value="Elem" /><font class="sm">Elem</font> <input type="checkbox" name="lev[]" value="MS" /><font class="sm">MS</font> <input type="checkbox" name="lev[]" value="HS" /><font class="sm">HS</font> <input type="checkbox" name="lev[]" value="College" /><font class="sm">College</font> <input type="submit" value=" ..:: Submit ::.. " /> Thank you. ~Wayne Quote Link to comment https://forums.phpfreaks.com/topic/211274-echo-checked/ Share on other sites More sharing options...
Psycho Posted August 20, 2010 Share Posted August 20, 2010 Ideally you would want to "build" the checkboxes from wherever you store the list (in a DB?). Without knowing your entire application here is a solution using an array. I also modified it slightly so you can give the checkboxes more descriptive labels other than "MS" while still preserving the values. I'm not sure why you have a closing FORM tag before the form fields, but oh well. Lastly, don't use the FONT tag, it has been deprecated for years. Since you are using a class it isn't doing anything anyway - just use a span tag. <form /> <?php $levelList = array( 'PreK' => 'Pre Kindergarten', 'Elem' => 'Elementary', 'MS' => 'Middle School', 'HS' => 'High School', 'College' => 'College' ); //Display selected levels echo "<b>Levels <br /> />"; echo implode(" <br />\n", $_GET['lev']); //Create list of levels for selection foreach($levelList as $level => $label) { echo "<input type=\"checkbox\" name=\"lev[]\" value=\"{$level}\" />"; echo "<span class=\"sm\">PreK</span>\n"; } ?> <input type="submit" value=" ..:: Submit ::.. " /> Quote Link to comment https://forums.phpfreaks.com/topic/211274-echo-checked/#findComment-1101644 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.