Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get the selected content of my listbox? 1

Status
Not open for further replies.

Quickfix99

Technical User
May 16, 2007
27
CA
I have some code that populates a list box from a database. I am trying to assign the value of the list box to a variable when the user clicks on a button. For some reason, my code does not set the variable. Any ideas? Here is the code I am using:

<?php
require("phpsqlajax_dbinfo.php");

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

$connection=mysql_connect (localhost, $username, $password);
if (!$connection) { die('Not connected : ' . mysql_error());}

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

$query="SELECT fullname FROM deceased ORDER BY fullname";
$result = mysql_query ($query);
echo "<select name=\"Deceased\">";// Start list box

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[fullname]>$nt[fullname]</option>";

}
echo "</select>";// Closing of list box

$select = $_POST['select'];
$chooser = $_POST['Deceased'];
echo($chooser.'<br><br>');

?>

<form action="testphp.php5" method="POST" >
<p><input type="Submit" name="Select" value="select" /></p>

</form>
</body>
</html>

Thanks,

QF

 
Your Select box is outside of your form, so it doesn't get sent when you press your submit button.

You need to place you echo statement for your select box inside the form tags.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ahhh...of course...

Now that it works, I have perhaps a tricker question. The data in my list is in the format:

"Penner, Fred"
"Pitt, Brad"

The data getting sent to the variable is only the text up to the comma. Does this make sense?

QF
 
Yes, you need to put quotes around your select option values thusly:

Code:
echo "<option value=[red]'[/red]$nt[fullname][red]'[/red]>$nt[fullname]</option>";

Notice the red single quotes around your $nt variable.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top