php 从下拉菜单中选择并重新加载页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18179067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select from drop-down menu and reload page
提问by Seth Earby
I've got a table that populates data from a MYSQL database and populates a drop-down menu from the same database. I have the drop down menu and table just fine, I would like to be able to choose which data I show in the table however.
我有一个表,它从 MYSQL 数据库填充数据并从同一数据库填充下拉菜单。我有下拉菜单和表格就好了,我希望能够选择我在表格中显示的数据。
<select name = 'peer-id' method='post' style = 'position: relative'>
<?php
while ($content = mysql_fetch_array($peer)) {
echo "<option value='" . $content['Peer'] . "'>" . $content['Peer'] . "</option>";
}
$results = mysql_query("SELECT Destination FROM rate ");
?>
</select>
That's what I have for the select box. How can I get the choice from that and save that as a variable and refresh the table data?
这就是我的选择框。如何从中获得选择并将其保存为变量并刷新表数据?
I need to clarify that this will change that current data
我需要澄清这将改变当前数据
#Data#Data#Data
#Data#Data#Data
#Data#Data#Data
Then choose drop down choice and I want it to show new data
然后选择下拉选项,我希望它显示新数据
#Data2#Data2#Data2
#Data2#Data2#Data2
#Data2#Data2#Data2
So it's going to need to load a new page or refresh some how because it's changing via PHP and not javascript.
所以它需要加载一个新页面或刷新一些方式,因为它是通过 PHP 而不是 javascript 更改的。
回答by Hanfeng
I think form
may be better, for example
我认为form
可能会更好,例如
<form id="myform" method="post">
<select name = 'peer-id' style = 'position: relative' onchange="change()">
<option value="1">12</option>
<option value="2">15</option>
<option value="3">16</option>
<option value="4">18</option>
</select>
</form>
<script>
function change(){
document.getElementById("myform").submit();
}
</script>
In the above code, whenever you change the value of select, it will post to the backend, then according to the posted value, you can do want you want, to get the peer-id in php, you can use the following code
在上面的代码中,每当你改变select的值时,它会发布到后端,然后根据发布的值,你可以做你想要的,在php中获取peer-id,可以使用以下代码
$peer-id = $_POST['peer-id'];
Hope helps!
希望有帮助!
回答by Yogesh Prajapati
apply this code in select tag hope this works
在选择标签中应用此代码希望这有效
<select onchange="location = this.options[this.selectedIndex].value;" style="text-decoration:none;">
<option value="customers.php"></font></option>
</select>
回答by Mattis Bratland
insted of the static options, you can do it like this :) here you get all the options from the database. Just replace it with the static options
安装静态选项,您可以这样做:) 在这里您可以从数据库中获得所有选项。只需将其替换为静态选项
$peer = mysql_query("SELECT Peer FROM rate Group By Peer Where peer = 'variable'");
$result_peer = mysql_query($peer);
if($result_peer){
while($row_peer = mysql_fetch_array($result_peer)){
echo'<option value='.$row_peer['Peer'].'>'.$row_peer['Peer'].'</option>';
}
回答by joe
I agree in using form
, and with this you can echo back onto the page with a submit button (code tested):
我同意使用form
,这样您就可以使用提交按钮(经过代码测试)回显到页面上:
<form id="myForm" method="POST">
<select name="select" onchange="<?php echo $_SERVER['PHP_SELF'];?>">
<option value="N">No</option>
<option value="Y">Yes</option>
</select>
<input type="submit" name="formSubmit" value="Submit" >
</form>
<?php
if(isset($_POST['formSubmit']) ){
$var = $_POST['select'];
$query = "SELECT * FROM table_name WHERE DesiredField='$var'";
$result = mysql_query($query)
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$var2 = $row['FieldName'];
echo "First field: " . $var2 . "<br>";
// and so on for what you want to echo out
}
}
?>