我需要允许我的用户在下拉列表中选择一个选项值,页面将刷新页面,并且数据库中的列将在页面上更新。我该如何设置此行为?
<script language="JavaScript">
// this js autorehresh page if option was change
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
<?php
//at first we must connect to DB
//external
require ('connectDB.php');
?>
<form method="post">
<select name="selected" onChange="MM_jumpMenu('parent',this,1)">
<?php
**// echo all id in table to options dropdown menu**
$sql='SELECT id FROM lekcia1';
$result=mysql_query($sql) or die(mysql_error($db));
$a = 0;
while ($recording=mysql_fetch_array($result)){
$a ++;
echo '<option value="'.$recording['selected'].'"> '.$a.' </option>';
};
?>
</select>
</form>
**<?php
// query NOT WORK
$sql1='SELECT * FROM lekcia1 WHERE id='.$_GET['']; // Notice: Undefined index: selected
$result1=mysql_query($sql1) or die(mysql_error($db));
$recording1=mysql_fetch_array($result1);
?>**
<ul>
<li><?php echo $recording1['column1'];?></li>
<li><?php echo $recording1['column2'];?></li>
<li><?php echo $recording1['column3'];?></li>
</ul>
答案 0 :(得分:0)
查询仍然无法正常工作,所以这是我正确的代码。
<html>
<head>
<script language="JavaScript">
// this js autorehresh page if option was change
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
</head>
<body>
<?php
$host='localhost'; // this will ususally be 'localhost', but can sometimes differ
$dbname='phpmyadmin'; // the name of the database that you are going to use for this project
$user='root'; // the USERNAME that you created, or were given, to access your database
$password=''; // the PASSWORD that you created, or were given, to access your database
$db = mysql_connect($host, $user, $password) or die("Neda sa pripojit k MySQL serveru: " . mysql_error());
mysql_select_db($dbname, $db) or die(mysql_error($db));
mysql_query('SET NAMES UTF8');
mysql_query('SET COLLATION_CONNECTION=uft8_general_ci');
?>
<form method="post">
<select name="selected" onChange="MM_jumpMenu('parent',this,1)">
<?php
// echo all id in table to options dropdown menu**
$sql='SELECT id FROM lekcia1';
$result=mysql_query($sql) or die(mysql_error($db));
$a = 0;
while ($recording=mysql_fetch_array($result)){
$a ++;
echo '<option value="'.$recording['selected'].'"> '.$a.' </option>';
};
?>
</select>
</form>
<?php
// query NOT WORK
$sql1="SELECT * FROM lekcia1 WHERE id='".$_POST['selected']."'"; // Notice: Undefined index: selected
$result1=mysql_query($sql1) or die(mysql_error($db));
$recording1=mysql_fetch_array($result1);
?>
<ul>
<li><?php echo $recording['nazov_lekcie'];?></li>
<li><?php echo $recording['cislo_ulohy'];?></li>
<li><?php echo $recording['nazov_ulohy'];?></li>
</ul>
<?php
//at first we must connect to DB
//external
//require ('connectDB.php');
?>
</body>
</html>