drfkl66684 2013-10-26 18:47
浏览 533

如何从下拉列表中获取所选值

I am trying to use a dynamically generated dropdown list to populate a table. I have a drop down list that is generated from my database (it grabs all the years available for a specific player). I want to be able to select a year from the dropdown and have it update my table. I have the dropdown being generated, but I am not able to get the selected value from the dropdown. I have code below that I found here, but it doesn't seem to work. Here is the code I have so far:

<input name="update" type="submit" value="Update" />
</form>
<p></p>
<form  action="player_login.html">
<input type="submit" value="Logout" />
</form>
</div>

<div style="float: left">
    <p></p>
<h1>Player Stats</h1>
<table width="300" border="1" cellpadding="2" cellspacing="2">

<?php
    // get "id" field from player table
    $login_id = $_COOKIE["DB"];
    $id = "select id from player where login_id='$login_id';";
    $result1=mysql_query($id)  or die('Select1 Query failed: ' . mysql_error());
    $row = mysql_fetch_array($result1);

    // create a dropdown from stats table in db
    echo "--Select Year--";
        $years_query = "select year from stats where player_id='$row[id]';";
        $years = mysql_query($years_query, $connect);

        // fill array with db info
        $var = array();
        while ($row2 = mysql_fetch_array($years))
        {
            $var[] = $row2['year'];
        }

        // create dropdown
    echo'<select name="years" id="years">'; 
    // For each value of the array assign variable name "city" 
    foreach($var as $year)
    { 
            echo'<option value="'.$year.'">'.$year.'</option>'; 
    }    
    echo'</select>';

    // get selected option from dropdown
    $selected_key = $_POST['years'];
    $selected_val = $var[$_POST['years']];
    echo "<p></p>selected key: " . $selected_val; // this wont print anything???

    $search_query="select * from stats where player_id='$row[id]' and year=2013;";
    $result=mysql_query($search_query)  or die('Select2 Query failed: ' . mysql_error());
    $num_cols = mysql_num_fields($result);
    $line = mysql_fetch_row($result);

    // create table with results
    echo "<tr>";
    echo "<td>Year</td>";  
        $j=1;
        echo "<td><input name='$j' type='text' value='$line[$j]' size=20/></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>Total Points</td>";  
        $j=2;
        echo "<td><input name='$j' type='text' value='$line[$j]' size=20/></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td>PPG</td>";  
        $j=3;
        echo "<td><input name='$j' type='text' value='$line[$j]' size=20/></td>";
    echo "</tr>";


?> 

</table>
</div>
  • 写回答

3条回答 默认 最新

  • dongzha0813 2013-10-26 18:54
    关注

    I see that you are using $_POST, and why do you don't use a form?

    //This is for get the form
        echo '<script type="text/javascript">
            //<![CDATA[
            function get_form( element )
            {
                while( element )
                {
                    element = element.parentNode
                    if( element.tagName.toLowerCase() == "form" )
                    {
                        return element
                    }
                }
                return 0; //error: no form found in ancestors
            }
            //]]>
        </script>';
    
    //create a form
    echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
    // create dropdown; onchange will send the form when selected index changes...
    echo '<select name="years" id="years" onchange="get_form(this).submit(); return false;">'; 
        // For each value of the array assign variable name "city" 
        foreach($var as $year)
        { 
                echo'<option value="'.$year.'">'.$year.'</option>'; 
        }    
        echo'</select></form>';
    

    And that's all! :D

    I'm seeing too that you are using an unique form for update all the page... It's isn't work, because you only have a submit button and no more element in the form, please read that: http://www.w3schools.com/tags/tag_form.asp

    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题