dty5753 2013-12-29 19:33
浏览 22
已采纳

ID选择php / jquery [关闭]

I have an select option like this :

    for($i = 1; $i <= 5; $i++)
    {
        $body .= <<<EOT

    <form method="GET" action="page2.php">
    <select class="multiSelect" size="7" name="opt[]" id="{$i}">
    <option value="1">1</option>
    <option value="2">2</option>
    ...
    </select>
    </form>
EOT;
    }

In the second page page2.php

if (isset($_GET['opt']) && !empty($_GET['opt']))
    {

        foreach($_GET['opt'] as $opt)
            $html .= "You have choice the option $opt in the !!!! $i !!!! select <br />
" ;

    }

I want to get the id ($i) of each select in the page2.php ? Can You help me please ? Thanks

  • 写回答

2条回答 默认 最新

  • duandian8110 2013-12-29 19:43
    关注

    I think what you want is this:

    I moved the form tag out of the loop, so that all selects are included in one form, I then assigned a unique name to each form.

    $body .= '<form method="GET" action="page2.php">';
    
    for($i = 1; $i <= 5; $i++)
    {
        $body .= <<<EOT
            <select class="multiSelect" size="7" name="opt_$i[]" id="{$i}">
            <option value="1">1</option>
            <option value="2">2</option>
            ...
            </select>
    
    EOT;
    }
    $body.= '</form>';
    

    and on page2 you want to do something like this: here I use 2 loops, the first one is looping through your selects (I'm assuming you always have 5 selects), I generate the name of each form with opt_$i (will be opt_1 ... opt_5)

    With the second loop, I loop through all selected values that where selected in that select

    $html = 'your choices:<br/>';
    for($i = 1; $i <= 5; $i++)
        $sel = "opt_$i";    
        $html.= "in Select $i you selected:";
        foreach($_GET[$sel] as $opt){
            $html.= $opt;
        }
    }
    

    you def. want to make the output prettier, but that should be easy to do and just adds more code, making it harder to see the essential steps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决