Solved

PHP DROP DOWN POPULATED BY MYSQL

Posted on 2011-03-08
9
342 Views
Last Modified: 2012-08-14
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
Comment
Question by:NeoAshura
  • 5
  • 4
9 Comments
 
LVL 11

Expert Comment

by:patsmitty
ID: 35075044
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
 
LVL 11

Expert Comment

by:patsmitty
ID: 35075058
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
 
LVL 6

Author Comment

by:NeoAshura
ID: 35075269
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
 
LVL 11

Accepted Solution

by:
patsmitty earned 500 total points
ID: 35075379
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 6

Author Comment

by:NeoAshura
ID: 35075431
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
 
LVL 6

Author Comment

by:NeoAshura
ID: 35075436
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
 
LVL 6

Author Comment

by:NeoAshura
ID: 35075441
my bad two secs
0
 
LVL 6

Author Comment

by:NeoAshura
ID: 35075449
You sir are a legend 10/10 good instructions quick too.
0
 
LVL 11

Expert Comment

by:patsmitty
ID: 35075504
Glad to help!

Cheers
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now