Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.