Jump to content

Retain listbox value on form reload


jonnyuk3

Recommended Posts

Hi all,

 

I have a listbox on a form that gets it's data from a MySQL query, all of which is working fine.

I make a selection in my listbox and then submit the form which is reloading itself. However on the reload I would like the listbox to display the selection made by the user, instead it reverts to the first item in the list.

I have included the code, if someone could point me in the right direction that would be great.

I have searched these forums and tried a few things that are suggested but I still can't get it to work.

 

 

<?php
include("dbconn.php");

$query = "SELECT location.location_name, printer.model_number, location_printer.serial_number" . 
" FROM location, printer, location_printer" .
" WHERE location.location_id = location_printer.location_id" .
" AND printer.printer_id = location_printer.printer_id";
$result = mysql_query($query);

echo "<table>";
echo "<th>Location</th>";
echo "<th>Printer</th>";
echo "<th>Serial No</th>";

while(($row = mysql_fetch_array($result)))
{
    echo "<tr>";
    echo "<td>".$row['location_name']."</td>";
    echo "<td>".$row['model_number']."</td>";
    echo "<td>".$row['serial_number']."</td>";
    echo "</tr>";
}
echo "</table>";

if(isset($_POST['location']))
{
$loc = $_POST['location'];
echo "<p>Location = <b> $loc </b>";
} 

?>

<html>
<head>
<title>Loop Results</title>
</head>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<table>
        <tr>
        	<td>
            	<?php
	    	$query = "SELECT * FROM location ORDER BY location_name";
		$result = mysql_query($query);

		$default = $_POST['location'];

		echo "<p>Location: ";
		echo "<select name='location'>";
		while(($row = mysql_fetch_array($result)))
		{
			echo "<option value='{$row['location_id']}";

			//Selected value
			if($row['location_id'] == $default)
				echo " selected ";

			echo "'>{$row['location_name']}</option>\n";
		}
				echo "</select>";				
	?>                
            </td>
        </tr>
        <tr>
        	<td>
            	<input type="submit" />
            </td>
        </tr>
    </table>
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/257615-retain-listbox-value-on-form-reload/
Share on other sites

hi blacknight, thanks for the reply.

 

yes, the form will be expanded later. I just need to get this working first.

 

I tried what you suggested and it still doesn't work!!

This is where I added the code, is this correct??

 

echo "<p>Location: ";
				echo "<select name='location'>";
				while(($row = mysql_fetch_array($result)))
				{
					echo "<option value='{$row['location_id']}";

						//Default value
						if(isset($_POST['location']) && $row['location_id'] == $_POST['location'])
							echo " selected ";

					echo "'>{$row['location_name']}</option>\n";
				}
				echo "</select>";				

 

yea basicly i see that now ...

try this..

echo "<option value='{$row['location_id']}'";
//Default value
if(isset($_POST['location']) && $row['location_id'] == $_POST['location'])
echo " selected ";
echo ">{$row['location_name']}</option>\n";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.