开发者

How do I add x-number HTML-formatted items to my page?

I'm trying to use PHP to do this:

<if !empty($list) {echo
  .
  .
  .
?>

And get the result:

Additional options:
    <p><input type="checkbox" name="1">1</input><开发者_StackOverflow中文版/label></p>
    <p><input type="checkbox" name="2">2</input></label></p>
    .
    .
    .
    <p><input type="checkbox" name="n">n</input></label></p>
  </legend>
</fieldset>


Given the context of the question, I am guessing here. But it seems that the you do not understand the checkbox, given that you do not even assign it a value that and this would be a pain to loop through on the form processing end.

Assuming that $list is an array (borrowing some code from Gazler)

$cnt = count($list);
$checkBoxes = "";

for ($i=1; $i<$cnt; $i++) {
    $checkBoxes .= '<p><input type="checkbox" name="checkBoxes" value="' . $i . '">' . $i . '</input></label></p>' . PHP_EOL;
}

echo $checkBoxes . '</legend>' . PHP_EOL . '</fieldset>';

Then on your form processing side, it will be easy to loop through the checked boxes like so:

if (isset($_POST['checkBoxes'])) {
    foreach ($_POST['checkBoxes'] as $val) {
        // $val will contain the value of the selected boxes
    }
}

Using this system, it should get you to where you want to be.


Assuming $list is an array

for($i=1; $i<count($list); $i++)
{
 echo '<p><input type="checkbox" name="'.$i.'">'.$i.'</input></label></p>'."\n";
}

If not, please provide the contents of $list.


Part 1:

// echo the 3-bar, expanded series (alternating sequence), of the tags array
<legend>Additional Options:</p>
$cnt = count($list);
$checkBoxes = "";

for ($i=1; $i<$cnt; $i++) {
    $checkBoxes .= '<p><input type="checkbox" name="checkBoxes" value="' . $i . '">' . $i . '</input></label></p>' . PHP_EOL;
}

echo $checkBoxes . '</legend>' . PHP_EOL . '</fieldset>' . "\n" . </legend>;

Part 2:

// loop through the checkboxes
if (isset($_POST['checkBoxes'])) {
    foreach ($_POST['checkBoxes'] as $val) {
        // $val will contain the value of the selected boxes
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜