Link to home
Start Free TrialLog in
Avatar of NeoAshura
NeoAshuraFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP DROP DOWN POPULATED BY MYSQL

Hello Experts,

Thank you for your time,

Im really stuck here, What im after is a PHP drop down box which is populated from my MYSQL database. this database could consist of the following.

Asda
Tesco
Morrisons
Sainsburys

etc etc

The drop down needs to "select * from company" and place it into the drop down menu to be selected.

a button is then clicked which posts the selected STORE into the next page where it is SEARCHED for in the database and the STORE details are then listed inside a new page.

The Store details would be located in another table for example.

Company_address

Asda 13 Leeds court
Tesco blah blah

You get the picture... based on the entry selected the following store address must be returned.. and none of the others..

Is this possible??

Many thanks

All input is appricated.

James
Avatar of Bruce Smith
Bruce Smith
Flag of United States of America image

Absolutely, Here is sample code for populating your drop-down box:
<select id="ddlCompanies">
<?php
$sql = "SELECT company_name FROM company ORDER BY company_name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
   echo ("<option>" . $row['company_name'] . "</option>");
}
?>
</select>
<br />
<input type="button" onclick="var val = document.getElementById('ddlCompanies').options[document.getElementById('ddlCompanies').selectedIndex].value; var url = 'companyInfo.php?val=';url+=val;window.location=url;" value="Submit..." />

Open in new window


Then to get the selected info from the db you could do something like this using the selected value from the URL parameter in the companyInfo.php page like this:
 
$val = $_GET['val'];
$sql = "SELECT store_address, store_phone FROM stores WHERE store_name = '" . $val . "'";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
   echo('Address: ' . $row['store_address']);
   echo('Phone: ' . $row['store_phone']);
}

Open in new window

With the code snippets above and a quick look through this tut here (http://www.tizag.com/mysqlTutorial/index.php) you'll be up and running in no time.
Avatar of NeoAshura

ASKER

thanks pats, i tried your code and nothing was clickable in the drop down..

any ideas why code is attached.
<?php
error_reporting(E_ALL);
include 'myphp.php';
?>
<select id="ddlCompanies">
<?php
$sql = "SELECT title FROM 'companies' ORDER BY title";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
   echo ("<option>" . $row['title'] . "</option>");
}
?>
</select>
<br />
<input type="button" onclick="var val = document.getElementById('ddlCompanies').options[document.getElementById('ddlCompanies').selectedIndex].value; var url = 'companyInfo.php?val=';url+=val;window.location=url;" value="Submit..." />

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bruce Smith
Bruce Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cheers works now as in i can select stuff but when i click go it returns following error code is attatched..

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\companyInfo.php on line

code
<?php 
error_reporting(E_ALL);
include 'myphp.php';
$val = $_GET['val'];
$sql = "SELECT store_address, store_phone FROM stores WHERE store_name = '" . $val . "'";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
   echo('Address: ' . $row['store_address']);
   echo('Phone: ' . $row['store_phone']);
}
   ?>

Open in new window

my bad two secs
You sir are a legend 10/10 good instructions quick too.
Glad to help!

Cheers