Connecting Tech Pros Worldwide Help | Site Map

How to use a value from a pick list?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:18 AM
dogu
Guest
 
Posts: n/a
Default How to use a value from a pick list?

I'd like to have a form that does the following:
User picks a site from a dropdown list from a MySQL db (that works -
code below, thanks to all those on the web who posted code for this).
Once the value is selected, a second query is run using the site as the
'where' clause and the results are displayed back to the user.

Any help for this mysql, php, htlm noob. I think once I get this bit,
I'll be able to move forward with more interesting and complex code.

TIA

Doug

ps - if this message shows up 3 times, my apologies but I've been having
trouble posting.

<html>
<body>
<?php

// connect to db
$db = mysql_connect("localhost");
mysql_select_db("test",$db);

// build query
$sitequery = "SELECT site from sitedata order by site";

// generate result set
$siteresult = mysql_query($sitequery);

// use results
echo "Select a site from the list below: <br><br><select name='site'>";
while($siterow = mysql_fetch_array($siteresult))
echo "<option
value='".$siterow["site"]."'>".$siterow["site"]."</option>";
echo "</select></td>";
?>

</body>
</html>

  #2  
Old July 17th, 2005, 04:19 AM
dogu
Guest
 
Posts: n/a
Default Re: How to use a ... figured it out.

Finally got the pieces to work together. For those of you who know what
you're doing, the following falls into the 'duh!' class. For anybody
else new to the game, I hope this helps.

Todo:
Disconnect the MySQL session.
Figure out a way to pass the db connection to the 2nd file rather than
reconnecting.
Convert everything to functions where it makes sense.
Play with other other form construction - embed php in html rather than
embedding html in php, figure out other methods to pass data between php
and html.

Dogu


Two files, both php.

First one returns a list of sites and launches the second form.
The second one uses the selected site name to return the password.

File 1:

<html>
<body>
<form action="getpw.php" method="get">

<?php

// connect to db
$db = mysql_connect("localhost");
mysql_select_db("test",$db);
$dbtbl = mysql_select_db("test",$db);

// build query
$sitequery = "SELECT site from sitedata order by site";

// generate result set
$siteresult = mysql_query($sitequery);

// use results
echo "Select a site from the list below: ";
echo "<br><br>";
echo "<select name='website'>";
while($siterow = mysql_fetch_array($siteresult))
echo "<option
value='".$siterow["site"]."'>".$siterow["site"]."</option>";
echo "</select></td>";

?>
<br>
<br>
<input type="submit" />
</form>
</body>
</html>

File 2 - getpw.php
The site you selected is <?php echo $_GET['website']; ?>.
<br>
<br>
The password is

<?php
// connect to db
$db = mysql_connect("localhost");
mysql_select_db("test",$db);

// build query
$pwquery = "SELECT password from sitedata where
site="."\"".$_GET['website']."\"";
// generate result set
$pwresult = mysql_query($pwquery);
$sitepw = mysql_data_seek($pwresult,0);
while($sitepw = mysql_fetch_array($pwresult))

echo $sitepw["password"];
echo "<br>";
echo "query is ".$pwquery;
?>
dogu wrote:[color=blue]
> I'd like to have a form that does the following:
> User picks a site from a dropdown list from a MySQL db (that works -
> code below, thanks to all those on the web who posted code for this).
> Once the value is selected, a second query is run using the site as the
> 'where' clause and the results are displayed back to the user.
>
> Any help for this mysql, php, htlm noob. I think once I get this bit,
> I'll be able to move forward with more interesting and complex code.
>
> TIA
>
> Doug
>
> ps - if this message shows up 3 times, my apologies but I've been having
> trouble posting.
>
> <html>
> <body>
> <?php
>
> // connect to db
> $db = mysql_connect("localhost");
> mysql_select_db("test",$db);
>
> // build query
> $sitequery = "SELECT site from sitedata order by site";
>
> // generate result set
> $siteresult = mysql_query($sitequery);
>
> // use results
> echo "Select a site from the list below: <br><br><select name='site'>";
> while($siterow = mysql_fetch_array($siteresult))
> echo "<option
> value='".$siterow["site"]."'>".$siterow["site"]."</option>";
> echo "</select></td>";
> ?>
>
> </body>
> </html>[/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.