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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to fill a dropdown, and capture the return? 1

Status
Not open for further replies.

dmkFoto

Programmer
Jul 9, 2007
20
US
I want to put a couple of dropdowns in my webpage.

The first dropdown will be filled with the categories of my products, with the first item "Select product category first!", then followed by the 10 different categories that I retreive from the database query.

Would appreciate some sample code.

And when user selected a specific category, how do I capture the specific items selected?

Thanks.

Alex
 
You have not told us what Db you are using, but as an example for a mysql db:

Code:
echo "<select name='mydropdown'>"; [green]\\start select tag for dropdown[/green]
while($row=mysql_fetch_array($resultset){ [green]\\loop through the query results from DB[/green]
echo "<option value='" . $row['somefieldname'] . "'>" . $row['anotherfieldname'] . "</option>"; [green]\\output each query row, as an option for the drop down. giving the option a value and a name [/green]
}
echo "</select>";

The selected option will contained in the variable for the select box's name, in this case being "$_POST['mydropdown']"


If you want the second drop down to get populated depending on what is selected from the first, you will need to submit the form, and parse the selection, and then create the dropdown, with the values returned from the query.

If you don't want the page to be refreshed, then you'll need javascript to fill in the drop down at the moment of selection.



----------------------------------
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.
 
Pardon my ignorance. This is my first dropdown so I need further clarification.

Here is the code of my drop down form in HTML:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<body>

<form method="POST" action="--WEBBOT-SELF--">
<!--webbot bot="SaveResults"
U-File="file:///C:/Documents and Settings/test/My Documents/My Webs/_private/form_results.txt"
S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<p><select size="1" name="D1">
</select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Where would you put your php loop ther to fill "D1", which is the name of the dropdown?

Thanks.
 
Code:
<form method="POST" action="--WEBBOT-SELF--">
  <!--webbot bot="SaveResults"
  U-File="file:///C:/Documents and Settings/test/My Documents/My Webs/_private/form_results.txt"
  S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
  <p><select size="1" name="D1">[red]
<?PHP
while($row=mysql_fetch_array($resultset){ [green]\\loop through the query results from DB[/green]
echo "<option value='" . $row['somefieldname'] . "'>" . $row['anotherfieldname'] . "</option>"; [green]\\output each query row, as an option for the drop down. giving the option a value and a name[/green]
}
?>
[/red]
  </select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

----------------------------------
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.
 
Thank you very much for the heads up.
One more question, in the above example, when I press the submit button, how do I capture the value selected?

Thanks.

Alex
 
When I run the following file name Form.php

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?php
if (isset($_POST['B1']))
{
echo "Hi ".$_POST['Category']." <br />;
}
?>

<form action="Form.php" method="post">
<p>
<select size="1" name="Category">

<option value='Continuous Lights'>Cont. Lights</option>
<option value='Studio Strobes'>Strobes</option>
<option value='Backgrounds'>Background/Backdrop</option>
<option value='Video Tripods'>Video</option>

</select><input type="submit" value="Go" name="B1"></p>
</form>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I got the following error:

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/Form1.php on line 10

Line 10 refers to:

<form action="Form.php" method="post">

I can't see what "," or ";" is needed in this line.

Hope another pair of eyes can find where the error lies.

Thanks.

Alex
 
You're missing the closing double quote on this line:
Code:
        echo "Hi ".$_POST['Category']." <br />[!]"[/!];
On line 10, PHP parser found the closing quote and then it got completely confused after that.

___________________________________________________________
[small]Do something about world cancer today: PACT[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top