Jump to content

echo checked


wkilc

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/211274-echo-checked/
Share on other sites

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 ::.. " />

Link to comment
https://forums.phpfreaks.com/topic/211274-echo-checked/#findComment-1101644
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.