How to get value from Combo Box PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
VMthinker
Forum Newbie
Posts: 6
Joined: Sun Sep 12, 2010 8:50 pm

How to get value from Combo Box PHP?

Post by VMthinker »

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:

Code: Select all

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

Code: Select all

<?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!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How to get value from Combo Box PHP?

Post by Jonah Bron »

So, is there a problem? Does echo.php function as expected?
VMthinker
Forum Newbie
Posts: 6
Joined: Sun Sep 12, 2010 8:50 pm

Re: How to get value from Combo Box PHP?

Post by VMthinker »

Jonah Bron wrote:So, is there a problem? Does echo.php function as expected?
The "echo.php" only echos out "data is : <blank>". By right the output should be "data is : <teacher/student>"

Thanks.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: How to get value from Combo Box PHP?

Post by Jonah Bron »

Problem #1: the action for your form is set to select_table.php, not echo.php.
VMthinker
Forum Newbie
Posts: 6
Joined: Sun Sep 12, 2010 8:50 pm

Re: How to get value from Combo Box PHP?

Post by VMthinker »

Jonah Bron wrote:Problem #1: the action for your form is set to select_table.php, not echo.php.
Changed the action to "echo.php" and it still doesn't work.

I re-scripted my codes which should be easier to understand and less prone to the first error:

Code: Select all

<td valign=top><strong>Name:</strong></td>

<?php
include "db_connect.php";

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

$action=mysql_query("show tables");

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

if(mysql_num_rows($action)) 
{ 
    while($table_array = mysql_fetch_array($action)) 
    { 
        echo "<option>$table_array[0]</option>";
    }
    
    echo'</select><br>';   
    
    $word = $_POST['table'];
    
    if(!$_POST['submit'])
    {
      echo '<br><input type="submit" name="submit" value="Submit">'; 
    }
    
    else
    {
      echo '<script type="text/javascript">
            alert("Redirecting you to echo.php page");
            window.location="echo.php"</script>';
    }
    
    echo '</form>';
}

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

?>
VMthinker
Forum Newbie
Posts: 6
Joined: Sun Sep 12, 2010 8:50 pm

Re: How to get value from Combo Box PHP?

Post by VMthinker »

The answer has finally been found! The script located at the top is fine, just needs the missing touch of $_SESSION.

Thanks Jonah for helping with a bit of the scripting.

The script for those interested: "echo.php"

Code: Select all

<?php
session_start();

if($_SESSION['id'])
{
    echo "Welcome ",$_SESSION['username']."<br>";
    echo "first data is : ".$_SESSION['table_choice']."<br>";
    echo "data is : ".$_SESSION['table_choice']."<br>";
    
    echo "<br>Click here to Logout :    ".'<br><a href="logout.php">Logout<br></a>';
}

else
{
    echo "You don't belong here!";
        
    echo '<script type="text/javascript">
         alert("Redirecting you to the site main page");
         window.location="index.php"</script>';
}
?>
Post Reply