问题
My code now works. Thanks everyone! Here is the final code in case anyone else ends up having the same trouble as I did.
<?php
mysql_connect('localhost', 'root', 'password');
mysql_select_db('database');
$query = "SELECT * FROM classroom";
$result = mysql_query($query);
?>
<select name="select1" id="select1">
<?php
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<option value="<?php echo $line['classname'];?>"> <?php echo $line['classname'];?> </option>
<?php
}
?>
</select>
<html>
<script type="text/javascript">
var urlmenu = document.getElementById( 'select1' );
urlmenu.onchange = function() {
window.open( "url/viewclass.php?classname=" + this.options[ this.selectedIndex ].value );
};
</script>
</html>
回答1:
in your html the value attribute of the MATH option is empty, fill it like your php code above.
you should place viewclass.php?classname=
in open
method and concatenate it with selected item.
HTML:
<select name="menu1" id="menu1">
<option value="MATH 2072">MATH 2072</option> <!-- php script should fill value -->
<option value="SCIENCE 1072">SCIENCE 1072</option> <!-- php script should fill value -->
</select>
JS:
var urlmenu = document.getElementById( 'menu1' );
urlmenu.onchange = function() {
window.open( 'viewclass.php?classname=' + this.options[ this.selectedIndex ].value );
};
Here's a working jsfiddle
回答2:
Call function on onchange
of select:-
<select name="menu1" id="menu1" onchange="myFunction()">
<option value="">MATH 2072</option>
</select>
Create function :-
<script type="text/javascript">
function myFunction(){
var urlmenu = document.getElementById( 'menu1' );
urlmenu.onchange = function() {
window.open( this.options[ this.selectedIndex ].value );
};
}
</script>
来源:https://stackoverflow.com/questions/33873478/how-to-redirect-to-another-page-in-php-based-on-drop-down-selected-value-text