列表框应该下拉

发布于 2024-11-03 18:37:03 字数 717 浏览 0 评论 0原文

对每一个人 我的数据库中有一些值,我想用复选框显示它们。 当我单击按钮时应该显示这些值。这不应该出现在组合框中。 因为我想一次发布多个值。 请帮忙解答谢谢

<?php

$womenlist=mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
$gs=0;
while($girlslist=mysql_fetch_array($womenlist))
{
    $gs++;
?>
<li style="background-color:#CCC; width:150px;"><label for="chk1"><input type="checkbox" name="chk_<?php echo $gs?>" id="chk<?php echo $gs?>"  value="<?php echo $girlslist['calName'];?>" <?php if($_REQUEST['chk_'.$gs]==$girlslist['calName']){?> checked="checked"<?php }?>><?php echo $girlslist['calName']." ".$girlslist['calDesc']; ?>  </label></li>
<?php }?>

to every one
I have some values in my data base i Want to display them with check boxes.
those values should be display when i click at the button. This should not in combo box.
because I want to post multiple values at one time.
Please help with thanks

<?php

$womenlist=mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
$gs=0;
while($girlslist=mysql_fetch_array($womenlist))
{
    $gs++;
?>
<li style="background-color:#CCC; width:150px;"><label for="chk1"><input type="checkbox" name="chk_<?php echo $gs?>" id="chk<?php echo $gs?>"  value="<?php echo $girlslist['calName'];?>" <?php if($_REQUEST['chk_'.$gs]==$girlslist['calName']){?> checked="checked"<?php }?>><?php echo $girlslist['calName']." ".$girlslist['calDesc']; ?>  </label></li>
<?php }?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的笑 2024-11-10 18:37:03

您可以限制查询中的列数(不是 SELECT * ...)。您已将 放入 内。 for="" 属性被硬编码为 chk1。您可以取出

  • 上的内联 style="" 并将其放入样式表中。我已经“整理”了一下(未经测试):
  • $womenlist = mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
    $gs = 0;
    while( $girlslist = mysql_fetch_array($womenlist) )
    {
    $gs++;
    echo '<li style="background-color:#CCC; width:150px;">'
       . '<label for="chk' . $gs . '">' . $girlslist['calName'] . ' ' . $girlslist['calDesc'] . '</label>'
       . '<input type="checkbox" name="chk_' . $gs . '" id="chk' . $gs . '" value="' . $girlslist['calName'] . '"
       . (($_REQUEST['chk_'.$gs]==$girlslist['calName']) ? 'checked="checked"' : '') . '></li>';
    }
    

    You could limit the number of columns in your query (not SELECT * ...). You've put the <input> inside the <label>. The <label>'s for="" attribute is hardcoded as chk1. You could take out the inline style="" on the <li> and put it into a stylesheet. I've "tidied" it up a bit (untested):

    $womenlist = mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
    $gs = 0;
    while( $girlslist = mysql_fetch_array($womenlist) )
    {
    $gs++;
    echo '<li style="background-color:#CCC; width:150px;">'
       . '<label for="chk' . $gs . '">' . $girlslist['calName'] . ' ' . $girlslist['calDesc'] . '</label>'
       . '<input type="checkbox" name="chk_' . $gs . '" id="chk' . $gs . '" value="' . $girlslist['calName'] . '"
       . (($_REQUEST['chk_'.$gs]==$girlslist['calName']) ? 'checked="checked"' : '') . '></li>';
    }
    
    你列表最软的妹 2024-11-10 18:37:03

    不完全确定您在这里要求什么(因为看起来您已经使用复选框进行了此操作),但实际上您可以使用选择框发布多个值。您只需使用 multiple 属性,并将名称指定为带有方括号的数组:

    <form action="yourscript.php" method="post">
      <select name="women[]" multiple="multiple">
        <option value="woman1_name">Woman 1</option>
        <option value="woman2_name">Woman 2</option>
        <option value="woman3_name">Woman 3</option>
      </select>
      <input type="submit" />
    </form>
    

    如果您使用此方式发布表单,请选择 Woman 2 & 3、var_dump($_POST); 产生:

    array(1) {
      ["women"]=>
      array(2) {
        [0]=>
        string(11) "woman2_name"
        [1]=>
        string(11) "woman3_name"
      }
    }
    

    或者,如果您希望复选框的值以类似的方式显示,请更改它们,使它们都具有相同的名称,但末尾带有方括号。此 HTML 将生成类似的 POST 数据:

    <input type="checkbox" name="women[]" value="Woman 1" />
    <input type="checkbox" name="women[]" value="Woman 2" />
    <input type="checkbox" name="women[]" value="Woman 3" />
    

    因此,要使用它创建下拉列表,请对您的代码进行修改。我相信这就是你所追求的:

    <?php
    $options = '';
    $womenlist=mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
    while($woman=mysql_fetch_array($womenlist)) {
        $options .= '<option value="'.$woman['calName'].'"'.((!empty($_REQUEST) && in_array($woman['calName'],$_REQUEST['women'])) ? ' selected="selected"' : '').'>'.$woman['calName'].' '.$woman['calDesc'].'</option>';
    }
    ?>
    <label for="women">Women:</label>
    <select id="women" name="women[]" multiple="multiple">
        <?php echo $options; ?>
    </select>
    

    Not sure entirely what you're asking for here (as it looks like you already have this working with checkboxes), but you can in fact post multiple values with a select box. You just use the multiple attribute, and specify the name as an array with the square brackets:

    <form action="yourscript.php" method="post">
      <select name="women[]" multiple="multiple">
        <option value="woman1_name">Woman 1</option>
        <option value="woman2_name">Woman 2</option>
        <option value="woman3_name">Woman 3</option>
      </select>
      <input type="submit" />
    </form>
    

    If you post a form with this, selecting woman 2 & 3, var_dump($_POST); yields:

    array(1) {
      ["women"]=>
      array(2) {
        [0]=>
        string(11) "woman2_name"
        [1]=>
        string(11) "woman3_name"
      }
    }
    

    Alternatively, if you want the values of your checkboxes to come through in a similar fashion, change them so they all have the same name, but with the square brackets on the end. This HTML would yield similar POST data:

    <input type="checkbox" name="women[]" value="Woman 1" />
    <input type="checkbox" name="women[]" value="Woman 2" />
    <input type="checkbox" name="women[]" value="Woman 3" />
    

    So, to create a dropdown using this, here's an adaptation of your code. I believe this is what you're after:

    <?php
    $options = '';
    $womenlist=mysql_query("select * from tbl_cycleduraion where user_id=$_SESSION[user_id]");
    while($woman=mysql_fetch_array($womenlist)) {
        $options .= '<option value="'.$woman['calName'].'"'.((!empty($_REQUEST) && in_array($woman['calName'],$_REQUEST['women'])) ? ' selected="selected"' : '').'>'.$woman['calName'].' '.$woman['calDesc'].'</option>';
    }
    ?>
    <label for="women">Women:</label>
    <select id="women" name="women[]" multiple="multiple">
        <?php echo $options; ?>
    </select>
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文