Jump to content

Echoing a value from a populated drop-down list.


DVigneault

Recommended Posts

Hey--I'm new to the forum, to php, and to web development in general.  I am using php to populate a drop-down list from a MySQL database, and then echo the text in a field associated with the selected value below.  I'm having no trouble creating the drop-down list, but I can't figure out how to echo the associated field below, or the ?Abbreviation=value that I expected in the url.

 

Here is the php file:

 

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<form name="form" method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select>
<?php

$con = mysql_connect("localhost","root","root");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("calculators", $con);

$result = mysql_query("SELECT * FROM calculator");

while($row = mysql_fetch_array($result))
  {
echo "<option type='text' name='Title' " . "value='" . $row['Title'] . "'>" . $row['Abbreviation'] . "</option>";
  }
mysql_close($con);
?>
</select>
<br>
<input type="submit" name="submit">
</form>
    
<?php echo $_GET['Title']; ?>

</body>
</html>

 

This is what the source code looks like in the browser:

 

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>

<form name="form" method="GET" action="/calculators/index.php">
<select>
<option type='text' name='Title' value='Mean Arterial Pressure'>MAP</option><option type='text' name='Title' value='Body Mass Index'>BMI</option></select>
<br>
<input type="submit" name="submit">
</form>
    

</body>
</html>

 

And this is appearing in the url after I press submit:

 

?submit=Submit

 

I'm sorry if this is a silly question, or if I'm being long-winded in asking it.  I'm very new to web development, and I'm not sure exactly what information is necessary to solve the problem.  Also, though changes to the code are helpful, I may need an explanation to understand them...

 

Thanks,

 

Davis

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.