Solved

PHP DROP DOWN POPULATED BY MYSQL

Posted on 2011-03-08
9
346 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:Bruce Smith
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:Bruce Smith
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 11

Accepted Solution

by:
Bruce Smith 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
 
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:Bruce Smith
ID: 35075504
Glad to help!

Cheers
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I fix this UPDATE error? 7 32
Optimize the query 5 42
How to obtain the string from a PHP StdObject ? 6 33
Magento: different theme for Pc and for mobile devices 4 31
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

809 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