I have created a drop down list from a SQL query as follows:

<?php
	echo '<form action ="boat_info.php" method="POST">';
	$query = "select SHIP_NAME from SHIP";
	$result = mysql_query($query, $link);
	if (mysql_num_rows($result)) 
	{
		echo "<select name='boat' value='bname'>Ship Name: </option>";

		while ($row = mysql_fetch_row($result))
		{
			print("<option value=\"$row[0]\" name =\"bname[]\">$row[0]</option>");
		}
	}
	else
	{
		print("<option value=\"\">No boats found</option>"); 
	}
	echo '<input type="submit" value="Find Cruises!">';
	echo '</form>'
	?>

And it lists a drop down menu with all the ship names as i had planned. Now i want to be able to click the 'Find Cruises' button which will then take the selected ship from the pull down menu - pass that into a sql query which will then list all the cruises available for that particular boat... is this possible?

IE - how do i get the information about the highlighted option in the pull down --- i have a seperate .php file that deals with the form - but i cannot seem to use $_POST[bname] as a variable!

What am i doing wrong! Thank you!

Recommended Answers

All 2 Replies

tones1986,

I'm really new to PHP and I can't see where you actually set your variables, but instead of trying to use $_POST[bname] (I can only assume you are) .... assign a variable to it such as $bname=$_POST['bname']; then just reference the variable $bname in your script.

Also, Where is the code you are trying use $_POST[bname] ? The drop down list input name="?"

You could try something like this:

<?php
//connect  to the database 
	$dbcnx = @mysql_connect('localhost', 'user_name', 'password');
if (!$dbcnx) 
{
exit('<p>Unable to connect to the ' .
'database server at this time.</p>');
}
if (!@mysql_select_db('table_name')) 
{
exit('<p>Unable to locate the  ' .
'database at this time.</p>');
}

// Populate Drop Menu lookup_boat_info

$sql="SELECT * FROM ship"; 
$result=mysql_query($sql); 

$options=""; 

while ($row=mysql_fetch_array($result)) 
{
    $bname=$row['ship_name']; 
    $options.="<OPTION VALUE=\"$bname\">".$bname; 
} 
?>
<SELECT NAME="ship_name"> 
<OPTION VALUE=0>Please Choose a Ship
<?=$options?> 
</SELECT>

This will populate your drop down from a table named "ship" given that table is available, but it looks like you figured that part out.

Your form boat_info.php will handle your SELECT query. Make sure you assign your variable such as $bname=$_POST['bname']; and use that variable in your query stipulating whatever criteria you want to search by.

An example could be :

//-query  the database table SHIP
	$sql="SELECT  * FROM ship WHERE ship_name=" . $bname; 
	
	//-run  the query against the mysql query function 
	$result=mysql_query($sql); 
	
	//-create  while loop and loop through result set 
	while($row=mysql_fetch_array($result))
	{ 
	  			$bname=$row['ship_name'];
		// You can assign other variables as well to correspond to your table data
                                $date=$row['date_available'];
                                // etc. 
						
	//-display  the result of the array 
        // I applied a font color and displayed the results in an unordered list 
	
	echo  "<ul>";
	echo  "<li><font color='red'>Ship Name:</font> ". $bname . "</li>\n";
        echo  "<li><font color='red'>Date Available:</font> ". $date . "</li>\n";
	echo  "</ul>";

That should do it or am I totally missing the point here?

I assume that ship listing page is ship.php

echo '<form action ="boat_info.php" method="POST">';
echo "<select name='bname' id='bname'>
<option>Ship Name: </option>";
	$query = "select SHIP_NAME from SHIP";
	$result = mysql_query($query, $link);
	if (mysql_num_rows($result)) 
	{
		

		while ($row = mysql_fetch_row($result))
		{
			echo ("<option value=\"$row[0]\" >$row[0]</option>");
		}
	}
	else
	{
		echo ("<option value=\"\">No boats found</option>"); 
	}
	echo '<input type="submit" value="Find Cruises!">';
	echo '</form>'

When clicked on Find Cruises it submits to boat_info.php

if(isset($_POST['bname']))
{



	$query = "select CRUISE_NAME from CRUISE WHERE SHIPNAME='".$_POST['bname']."'";
	$result = mysql_query($query, $link);
	if (mysql_num_rows($result)) 
	{
		

		while ($row = mysql_fetch_row($result))
		{
			echo ("Cruise name : ". $row[cruise_name]);
		}
	}




}

Hope above code helps.

Regards,
Satish
<FAKE SIGNATURE>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.