[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

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
0
NeoAshura
Asked:
NeoAshura
  • 5
  • 4
1 Solution
 
Bruce SmithSoftware Engineer IICommented:
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

0
 
Bruce SmithSoftware Engineer IICommented:
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.
0
 
NeoAshuraAuthor Commented:
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

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Bruce SmithSoftware Engineer IICommented:
When you say nothing is 'clickable', is the drop-down list empty?

Change line 7 to:  
$sql = "SELECT title FROM companies ORDER BY title";

Open in new window

0
 
NeoAshuraAuthor Commented:
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

0
 
NeoAshuraAuthor Commented:
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

0
 
NeoAshuraAuthor Commented:
my bad two secs
0
 
NeoAshuraAuthor Commented:
You sir are a legend 10/10 good instructions quick too.
0
 
Bruce SmithSoftware Engineer IICommented:
Glad to help!

Cheers
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now