简体   繁体   English

php填写动态列表中的选定内容

[英]php filling in selected in a dynamic list

My question extends on the answer on this post and I couldn't find what I'm looking for posted otherwise. 我的问题是关于此帖子的答案的,而我找不到发布的其他内容。

How do I auto select (checked) options in a checkbox list when the options are being pulled from a mySQL table (dynamic)? 从MySQL表(动态)中提取选项时,如何在复选框列表中自动选择(选中)选项?

Here's what I have so far: 这是我到目前为止的内容:

Pulling the committees (comms) from the table: 从表中拉出委员会(通讯):

while ($row = mysql_fetch_assoc($comm)){
    foreach ($row as $v){
        $comms[] = $v;
        }
}

outputting pretty HTML with tidy PHP (the part I need help with): 用整洁的PHP输出漂亮的HTML(我需要帮助的部分):

    foreach ($comms as $comm){
       ?????????
    }

How can I output something like?: 如何输出类似的内容?:

<input type="checkbox" name="committee" value="blue" checked="checked" />blue<br />
<input type="checkbox" name="committee" value="green"  />green<br />
<input type="checkbox" name="committee" value="orange"  />orange<br />
<input type="checkbox" name="committee" value="purple" checked="checked" />purple<br />

Assuming your colors are stored in an array and your data comes back as an array (likely not the case if you're using mysql_* functions), you can print out each checkbox, then compare its value to see if it's in the list of "checked" colors: 假设颜色存储在数组中,并且数据作为数组返回(如果使用mysql_*函数,情况可能并非如此),则可以打印出每个复选框,然后比较其值以查看是否在列表中“选中”颜色:

<?php
$colors = array('blue', 'green', 'orange', 'purple');

$data = array('blue', 'purple');//array pulled from database.

foreach($colors as $color): ?>
    <input type="checkbox" name="committee" value="<?= $color ?>" <?= in_array($color, $data) ? 'checked="checked" ' : '' ?>/><br />
<?php endforeach; ?>

By the way, you should stop using mysql_* functions. 顺便说一句,您应该停止使用mysql_*函数。 They're being deprecated. 他们已被弃用。 Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). 而是使用PDO (自PHP 5.1起受支持)或mysqli (自PHP 4.1起受支持)。 If you're not sure which one to use, read this article . 如果您不确定要使用哪个,请阅读本文

There are a couple of things you need to check. 您需要检查几件事。 If someone's tried to submit the form already, then their responses will be in $_REQUEST (and $_POST or $_GET , depend on your form's submission method); 如果有人已经尝试提交表单,那么他们的回复将以$_REQUEST (和$_POST$_GET ,取决于您表单的提交方法)进行; you can then write each line as: 然后,您可以将每一行写为:

<input type="checkbox" name="committee[]" value="green" <?php if ($_REQUEST['committee'] == 'green') { echo "checked"; } ?> />green<br />

If it's coming from the database, you can check in $comms " 如果来自数据库,则可以签入$comms

<input type="checkbox" name="committee[]" value="green" <?php if (in_array($comms, 'green') { echo "checked"; } ?> />green<br />

You can make the code more efficient by working out in advance whether it's a re-submission or not, and populating a single array with the result from the right place: 您可以通过预先计算是否重新提交来提高代码效率,并使用正确位置的结果填充单个数组:

<?php

if ([form is submitted]) {
    $comms = $_REQUEST["committee"];
} else {
    $comms = getCommsFromDatabase();
}

?>

That also gives you the chance to sanitise the input from $_REQUEST. 这也使您有机会清理$ _REQUEST的输入。

while ($row = mysql_fetch_assoc($comm)){
    foreach ($row as $v){
        $comms = $v;
        }
}

where you can print values from assuming that $v has an attribute Checked 您可以在其中假设$ v具有Checked属性的情况下打印值

   foreach($coms as $key => $value)
   {
      $checked = "";
      $color = $value['color'];
      if($value['checked'] == 'checked')
      $checked = "checked = 'checked'";
      echo "<input type='checkbox' name='committee' value='$color' $checked />$color<br />";

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM