views:

247

answers:

2

Hi there! 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. Can someone please give some advice? Thanks!

A: 

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.

michaeltwofish
But I have seen people using $_POST[''] in php taking the value from some other .html file. So what you are referring is that the code etc. "$word = $_POST['table'];" be created in select_table.php instead of echo.php???
JavaNoob
+1  A: 

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>';
}
?>
WakiMiko
Yes your code works. but Is it possible that I can bring the "$_POST['table']" variable result to another page to be used for another query??
JavaNoob
Right answer. Just simply needs a $_SESSION['table_choice'] to transfer the POST value to the next php page.
JavaNoob