2012-03-22 41 views
0

好吧我已经得到了这么远的你的美妙帮助,只是试图找出我要去哪里错了吗?动态下拉 - 用于表格创建的用户选择。可能吗?

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

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

+0

哦我错过了''选定的文本'' – user1287030 2012-03-23 01:05:09

+0

尝试类似于什么我刚刚编辑到我的答案:) – Daan 2012-03-23 07:01:06

回答

0

更改以下行:

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

这样:

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

而且我认为它会正常工作。

+0

为什么你想要的价值每个选项都一样? – Aknosis 2012-03-22 21:59:43

+0

另外,在HTML中''。 – Daan 2012-03-22 22:09:19

0

至于写作HTML的一般性意见:你需要把<select><form>标签,并指定method属性有:<select>标签不具有这种性质。

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

你需要做的是这样的:你需要点在<select>标签的JavaScript函数读出在下拉列表中选择当前选择的onChange事件,然后通过这个到第二个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的更新版本:请仔细阅读jquery.com,而你正在阅读有关负载API)

+0

我有一个四处看看,到目前为止,这是我所想到的: