3

我正在尝试使用动态生成的下拉列表来填充表格。我有一个从我的数据库生成的下拉列表(它抓取了特定玩家可用的所有年份)。我希望能够从下拉列表中选择一年并让它更新我的表格。我正在生成下拉列表,但我无法从下拉列表中获取所选值。我有下面的代码,我在这里找到了,但它似乎不起作用。这是我到目前为止的代码:

<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>
4

3 回答 3

1

我看到您使用 $_POST 并且由于未提交表单,因此未设置 $_POST 的数据。我用来捕获事件并发送 AJAX 查询获取结果并更新它的最佳可用选项。

我在 J Query 的帮助下完成了这项工作,如下所示

$('#years').change(function() {
   $.ajax({
           //request of AJAX
    type : 'POST',
    url : 'players_data.php',
    dataType : 'json',
    data: {
        //Data with $_POST request
        years : $('#years').val();

    },
    success: function(data){
    //Things to be done with returned data
    }
}};

创建一个新文件 player_data.php 并在其中编写用于从数据库中获取数据的代码:

// 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);
$num_cols = mysql_num_fields($result);
$line = mysql_fetch_row($result);
$return['year']=$line;
echo json_encode($return);
于 2013-10-26T19:06:03.847 回答
0

我看到您正在使用$_POST,为什么不使用表单

//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>';

就这样!:D

我也看到你正在使用一个独特的表单来更新所有页面......这是行不通的,因为你只有一个提交按钮,表单中没有更多元素,请阅读:http://www .w3schools.com/tags/tag_form.asp

于 2013-10-26T18:54:28.163 回答
0

从您的代码中,我可以看到您想从选择框中获取值并立即填充表格并显示结果..使用 jquery 获取所选对象的值并将 javascript 变量分配给 php 变量。并插入数据库..

       <script type="text/javascript">
   $( "#years" ).change(function() {
     var value=document.getElementById("years").value;
         alert(value);
       </script>

将变量分配给 php 并执行您的 php 查询。

              <?php
             $data = "<script>document.write(value)</script>";
                  //execute  your query here..
        ?>

也看看ajax ..它做得很好......

于 2013-10-26T19:09:28.837 回答