JavaNoob
JavaNoob

Reputation: 3632

How to get value from Combo Box PHP?

I am using PHP 5 to create a query page for a MySQL database with 2 tables "students" and "teachers". I have created a combo box which can allow users to view and select the 2 tables from the combo box via a "submit" button after selecting from the combo box.

However the problem with the script is that I want to verify if the "submit" button works which I created a "echo.php" to echo out the value of the combo box and submit button. Overall the idea is to do a query like these steps:

1) User selects value from combo box "teacher" or "student". 2) User clicks submit button. 3) After clicking submit button, user is redirected to "echo.php" 4) "echo.php" should output/echo out either "teacher" or "student".

The codes for script:

<?php
include "db_connect.php";
{
?>
<td valign=top><strong>Name:</strong></td>
<td>

<?php
echo "<form name = \"queryEquipTypeForm\" method = \"post\" action
=\"select_table.php\">"; 
echo '<select name=\"table\">'; 
echo "<option size =30 selected>Select</option>";

$result = mysql_query("show tables");

if(!$result) trigger_error("Query Failed: ".  mysql_error($db), E_USER_ERROR);

if(mysql_num_rows($result)) 
{ 
while($table_array = mysql_fetch_array($result)) 
{ 
    echo "<option>$table_array[0]</option>";
}    

echo '</select>';

echo '</form>';

if(!$_POST['submit'])
{
?>
<form method="post" action="select_table.php">   
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
else
{
  echo '<script type="text/javascript">
        alert("Redirecting you to echo.php page");
        window.location="echo.php"</script>';
}

} 
else 
{
echo "<option>No Names Present</option>";  
} 
}

?>
</td>

The codes for "echo.php" :

<?php
include "select_table.php";
echo "data is : ".$_POST['table'];
?>

The output of echo.php would be exactly the same as "select_table.php" with the cobo box and the "data is: " without the "teachers" or "student" word as its being redirected to echo.php.

Upvotes: 2

Views: 18335

Answers (2)

jhenninger
jhenninger

Reputation: 707

I'm not quite sure what exactly what you want to do, but as michaeltwofish already pointed out, redirecting to another page using javascript will make you lose the post data. Also you seem to echo two <form> elements when the select_table.php is called without post data, one with only a submit button, while your first form is missing the button.

Normally, you would want your php script to either output the form with the combobox where the user can select the table, or, if there was post data (meaning that the user selected a value), the results of the selection, kinda like the following:

<?php

if(isset($_POST['table'])) {
    print "Selected element: " . $_POST['table'];
} else {
    echo '<form method="post" action="select_table.php">';
    echo '<select name="table">';
    // here should be your SQL query and the combobox options generation
    echo '</select>';
    echo '<input type="submit" />';
    echo '</form>';
}
?>

Upvotes: 3

michaeltwofish
michaeltwofish

Reputation: 4086

When the form is submitted, you redirect to echo.php, but that means you lose the POST data. You need to think carefully about the flow of your application, because what you have seems a bit confused.

Upvotes: 0

Related Questions