动态下拉 - 用于表创建的用户选择。可能吗?

时间:2012-03-22 21:50:08

标签: php

好的,我已经得到了你的精彩帮助,只是想找出我出错的地方吗?

    <script>
 function disp_text()
   {
   var w = document.myform.mylist.selectedIndex;
   var selected_text = document.myform.mylist.options[w].text;
   location.href="By_regions_report2.php?selected_text=" + selected_text; //loading a seperate php page to catch result
   alert(selected_text);
   }
   </script>
<?php

$dropdown_sql="SELECT DISTINCT `2010 RSG Code` AS codes FROM locations";
$dropdown_result=mysql_query($dropdown_sql);

$options="";

while ($row=mysql_fetch_array($dropdown_result)) {
    $codes=$row["codes"];
   $options.="<OPTION VALUE=\"codes\">$codes</option>";

     }

?>
<form NAME="myform">
<SELECT NAME="mylist" onchange="disp_text()">
<OPTION VALUE=0>Select a region
    <?php echo $options ?>
    </SELECT>
    </form>

这是第二个.php文件:

<?php

$selectedCode = $_GET[selected_text];
?>

获取错误使用未定义的常量selected_text - 在调用onchange()函数时假定为“selected_text”

编辑: 有没有办法自动重新加载原始页面中的新变量?

2 个答案:

答案 0 :(得分:0)

更改以下行:

$options.="<OPTION VALUE=\"codes\">$codes";

到此:

$options.="<OPTION VALUE=\"codes\">$codes</option>";

我认为它会正常工作。

答案 1 :(得分:0)

作为编写HTML的一般注释:您需要将<select>置于<form>标记内并在其中指定method属性:<select>标记没有此属性。

但是,在这种情况下,您要做的是根据用户在下拉框中的选择更改用户当前正在查看的页面内容。为此,您需要AJAX。

您需要做的是:您需要将onChange标记中的<select>事件指向一个Javascript函数,该函数会在下拉列表中读出当前选择,然后传递给它转到第二个PHP脚本,它返回您希望根据该选择向用户显示的任何数据。这使用AJAX。当AJAX请求返回时,您将在网页上显示此数据。你可以使用像jQuery这样的东西:我建议你使用Google并查阅一些教程:)

编辑:尝试这样的事情......

<html>
<head>
    <title>Test</title>
    <script src="ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
        function fetch_data() { 
           var selected_text = document.myform.mylist.options[w].text;
           $("#output").load("fetch_data.php", {code: selected_text});
        };
    </script>
</head> 
<body>
<?php
    $dropdown_sql="SELECT DISTINCT `2010 RSG Code` AS codes FROM locations";
    $dropdown_result=mysql_query($dropdown_sql);
    $options="";

    while ($row=mysql_fetch_array($dropdown_result)) {
        $code=$row["codes"];
        $options .= "<option value=\"codes\">$code ";
    }
?>
<form name="myform">
    <select name="mylist" onchange="fetch_data()">
    <option value="0 >Select a region
    <?php echo $options; ?>
    </select>
</form>
<div id="output">Output goes here</div>
</body>
</html>

创建一个文件并将其命名为fetch_data.php。此文件将处理在第一页的<div>中获取和格式化您要放入的数据:

<?php
    $selectedCode = $_POST['data'];
    //Do whatever you need to do to get the data you want to put into that <div> 
    //e.g. run a MySQL query and format the results in a table
    //the HTML this script generates will be put into the <div> directly so do not use <body> tags etc
    //Don't forget to escape the data in $_POST['data'] using mysql_real_escape_string to avoid SQL injection, so if you are using MySQL, connect to the database first
    //and then write $selectedCode = mysql_real_escape_string($_POST['data']); instead
?>

有关其工作原理的详细信息,请查看http://api.jquery.com/load/:)

(IIRC还有一个比你在微软使用的版本更新的jQuery版本:在你阅读有关加载API的时候查看jquery.com)