Hux
Hux

Reputation: 31

Maintain Dropdown Selection and Don't Submit Until Selection Made - PHP/MYSQL

I have created a dropdown menu to select the table, then another dropdown menu that is populated with data from from one column of the selected table. Currently, when I first load the page I get an error message as there is nothing selected in my dropdown so the index freqtype is undefined. When I select something everything works well, except my selection isnt retained by the dropbox. Is there a way within PHP to achieve this within the one script. I thought of maybe using an if statement for the initial form so the bottom dropdown doesnt populate until the top has been selected but I can't get the code to work. Any help would be greatly appreciated.

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

Upvotes: 0

Views: 768

Answers (2)

Tim Withers
Tim Withers

Reputation: 12059

You can check if $_POST['freqtype'] is set using PHP's isset() function and then not execute the rest of the code:

<?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>";
?>

On a side note, just because you are giving options in a drop down box, values other than what you decide could be submitted. This means that if you don't sanitize the input, you could have a very bad SQL experience.

Another note, you should look into PDO instead of using the very old mysql functions.

Upvotes: 0

lafor
lafor

Reputation: 12776

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

Upvotes: 2

Related Questions