0

我创建了一个下拉菜单来选择表格,然后是另一个下拉菜单,其中填充了来自所选表格一列的数据。目前,当我第一次加载页面时,我收到一条错误消息,因为我的下拉列表中没有选择任何内容,因此索引freqtype未定义。当我选择某些东西时,一切正常,除了我的选择没有被 Dropbox 保留。PHP中有没有一种方法可以在一个脚本中实现这一点。我想可能对初始表单使用 if 语句,因此在选择顶部之前不会填充底部下拉列表,但我无法使代码正常工作。任何帮助将不胜感激。

<html>
<head>
<title>Frequency Chart Update</title>
</head>

<body text="#FFFFFF" bgcolor="#000000">

<font size="4">Please Select the Frequency You Wish To Edit.
</font>
<br>
<br>
<?php

echo '<form ENCTYPE=multipart/form-data action=editfreq.php method=post>';
echo '<select name=freqtype onChange=this.form.submit()>';
echo '<option value="empty"> </option>';
echo '<option value="rptfreq">Repeater (Base) Frequencies</option>';
echo '<option value="pltfreq">Pilot Frequencies (VDV Leaky Feeder)</option>';
echo "</select>";

//define variables to be used
$table = $_POST['freqtype'];

//Connect to database
include 'connect.php';

$sql = "SELECT channel FROM $table";
$result = mysql_query($sql);


echo '<form ENCTYPE=multipart/form-data action=editrptfreq.php method=post>';
echo 'Channel  ';
echo '<select name=channel>';
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['channel'] . "'>" . $row['channel'] . "</option>";
}
echo "</select>";
echo "<button type=submit>Select</button>
";
echo "</form>";



?>

</body>
</html>
4

2 回答 2

2
<option value="rptfreq"<?php if(@$_POST['freqtype'] == 'rptfreq') echo ' selected="selected"'; ?>>Repeater (Base) Frequencies</option>
<option value="pltfreq"<?php if(@$_POST['freqtype'] == 'pltfreq') echo ' selected="selected"'; ?>>Pilot Frequencies (VDV Leaky Feeder)</option>
于 2012-07-20T22:09:08.700 回答
0

$_POST['freqtype']您可以使用 PHP 的函数检查是否已设置isset(),然后不执行其余代码:

<?php
$table = isset($_POST['freqtype'])?$_POST['freqtype']:false;
echo '<form ENCTYPE=multipart/form-data action=editfreq.php method=post>';
echo '<select name=freqtype onChange=this.form.submit()>';
echo '<option value="empty"> </option>';
echo '<option value="rptfreq" '.($table=='rptfreq'?'selected="selected"':''.).'>Repeater (Base) Frequencies</option>';
echo '<option value="pltfreq" '.($table=='pltfreq'?'selected="selected"':''.).'>Pilot Frequencies (VDV Leaky Feeder)</option>';
echo "</select>";

if($table){
    //Connect to database
    include 'connect.php';

    $sql = "SELECT channel FROM $table";
    $result = mysql_query($sql);
    echo '<form ENCTYPE=multipart/form-data action=editrptfreq.php method=post>';
    echo 'Channel  ';
    echo '<select name=channel>';
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row['channel'] . "'>" . $row['channel'] . "</option>";
    }
    echo "</select>";
}
echo "<button type=submit>Select</button>";
echo "</form>";
?>

附带说明一下,仅仅因为您在下拉框中提供选项,可以提交与您决定的值不同的值。这意味着如果您不清理输入,您可能会遇到非常糟糕的 SQL 体验。

另请注意,您应该研究 PDO 而不是使用非常旧的mysql函数。

于 2012-07-20T22:12:45.250 回答