View Full Version : Dropdown box that pulls from MYSQL database

06-21-2009, 12:13 PM
hi there

I need to create a "refine your search" function on a php page which consists of three dropdown boxes (box 1:brand, box 2: price, box 3:country) and when the user presses the "GO" button the correct products are pulled from corresponding fields in my MYSQL database and outputted onto the page.

The dropdown boxes do not need to populate from the database - they are static.

My php is basic, but I can write php functions to pull from my database onto a page. I think I might need some other scripts, such as JavaScript to do this too..???

My problem is pulling it all together and knowing where to start. I can't get my head around the fact that normally, your php page instructs to open your database, pull from it, and the products are returned on the page once it has loaded for the user, without any of the php instructions visible in the source code. With a dropdown, I don't understand how it all comes together, as the php instruction to pull from the databse will not take place until the user presses the "GO" button! Help - v confused!

Calling experienced coders....

Can anyone help, or point me in the direction of a tutorial (not an overview of php or similar) that goes through the process?

Many thanks in advance :)

06-22-2009, 05:31 AM
A quick search (http://www.google.com/search?hl=en&q=form+submit+OR+submission+process+OR+order) doesn't reveal anything so I'll try just explaining it...

When a browser (or, more generally, a client) requests a page from a server, some information is sent about exactly what is being requested. This includes the page name and optionally some variables. In a form submission, these variables are sent one of two ways, depending on the <form method> attribute, whether it is "get" or "post" (capitalized in the HTTP specification but not as HTML attribute values).

In a GET request, the variables are sent in the URL; this page has the GET variable 't' with a value of 46104. (If no variables are sent, the request is GET.) POST requests are almost identical to GET from a client's or server's perspective; the only real difference is that POST variables are normally hidden from the user. This is useful if the request is not idempotent (http://www.cs.tut.fi/~jkorpela/forms/methods.html) (i.e., doing more than just looking) or the variables contain sensitive information; most effects should not be preserved on the clipboard or in bookmarks for later use. Simple database queries (e.g., Google searches) are alright using GET, and this makes them much more useful than POST would.

So here's what happens in your scenario:
The client GET requests the ordinary form page.
The server processes any PHP (or other server-side language) in the requested file and sends the result.
The client submits the form by:
requesting the page specified in the form's 'action' attribute
with the variables from the form elements
using the request type specified in the form's 'method' attribute.

The server processes any PHP (or other server-side language) in the requested file and sends the result.

The part of all this which I think is confusing you is that your form's 'action' is the form page itself. This is a very common practice; the two pages are actually different because of the sent variables. If the form is submitted with GET, this will be more obvious. The coincidence is just a handy way of maintaining an HTML structure and some very similar PHP programs in only one file instead of two or more.

Ask questions... :)

06-22-2009, 08:22 PM
Here (http://teamtutorials.com/web-development-tutorials/pulling-drop-down-values-from-a-database-using-javascript-and-php)'s something someone else showed me in relation to his own problem today. It doesn't require AJAX, and I've made sure it works.

06-29-2009, 07:57 PM
I think that if you start the select then enter into a while loop then close it out that would work...

function builddropdown($connectionType, $connectionName, $connectionPass, $name, $dbTable, $dbField){
echo"<select name=$name >\n";
$con = mysql_connect($connectionType, $connectionName, $connectionPass);
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("$dbName", $con);
$result = mysql_query("SELECT * FROM $dbTable GROUP BY $dbField HAVING 0 < count( $dbField ) ORDER BY $dbField ASC");
while($row = mysql_fetch_array($result))
echo "<option value=".$row['FieldName']." >".$row['FieldName']."</option>\n";
echo "</select>\n";

just call this function in your form.

06-30-2009, 03:39 AM
Hi Elki. Sorry that I threw you off with that link; only the thread titles are similar between this and the one I found that on. The problem is completely different, and your post is just as off-topic as my second one.

07-02-2009, 10:42 PM
Doh, sorry.