Solved

PHP DROP DOWN POPULATED BY MYSQL

Posted on 2011-03-08
9
345 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Sessions in WordPress 8 30
Sending email via PHP & Pear - Warning: include_once(Net/SMTP.php): failed to open stream 5 54
PHP AJAX Wordpress 9 19
Php recording post 4 37
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

785 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