Click to See Complete Forum and Search --> : Help Search Function using Dropdown List
cyberjorge
10-09-2008, 04:53 AM
Hi, I have a scenario, that's need to be done in php please help me with the code. (consider me a total newbie)
I need to get a value from 2 static drop down lists and pass this on the server then return the result by displaying a static page.
I have 2 static drop down lists that users can choose then hit go. First you select "Property Type" then "Property Location" then the value should be grabbed and sent after clicking GO.
Then it should query with the database on the table "sitelinks" that includes the following columns: ID, Name and Link.
After checking, it should then show another static page as a result.
Below is a sample test of the result:
if ($type == 'condominium' && $location == 'click city')
display('condoclick.php');
else if ($type == 'residential' && $location == 'unclick city')
display('resunclick.php');[/CODE]
Thanks in advance!
sunnychotai
10-09-2008, 06:07 AM
Hi cyberjorge,
Can you paste code sample? If everything is static, then you will need to place the dropdowns within <form> tags and the Submit should use the $_GET method.
Your PHP should grab the values from the dropdown and query them. Depending on the results they will redirect to the relevant page using:
<?
header( "Location: typefilenamehere.php" );
?>
cyberjorge
10-09-2008, 09:34 PM
Hi sunnychotai,
Thanks for your response!
Here are my codes:
For the 2 dropdown lists and submit button in the index.htm:
<form id="form1" method="post" action="">
<select name="prop_type" size="1" id="prop_type" style="width: 140px;">
<option>- Property Type -</option>
<option>Office</option>
<option>Residential</option>
<option>Condominium</option>
<option>Leisure</option>
</select>
<select name="location" size="1" id="location" style="width: 140px;">
<option>- Location -</option>
<option>City1</option>
<option>City2</option>
<option>Province1</option>
<option>Province2</option>
</select>
</form>
<form id="form2" method="post" action="process.php">
<input type="submit" name="go" id="go" value="GO" />
</form>
This is for the process.php, I'm playing around but I know it needs to be corrected:
<?PHP
include('functions/config.php');
db_connect();
$TYPE = isset($_POST['prop_type']) ? $_POST : $_GET;
$LOC = isset($_POST['location']) ? $_POST : $_GET;
"SELECT * FROM sitelinks";
if ($type == 'Condominium' && $loc == 'City1')
display('condocity1.php');
else if ($type == 'Residential' && $loc == 'Province1')
display('resprov1.php');
?>
Please be patient with me as I am really new in php.
Thanks.
cyberjorge
10-10-2008, 03:37 AM
Anyone Please?
sunnychotai
10-10-2008, 04:43 AM
Hi,
A couple of things to note. The option tags should have an associated value. (as per below). This is what is sent to the PHP tags (not the actual text).
There should only be 1 form. You have two forms and when you click on the button it will only take over what is on form2.
Structure:
1. PHP to do the work
2. HTML form
I tend to assign the values sent from the form into variables and then play around with them. You will need to change the "filename.php" to whatever filename that you are calling this.
<?php
include('functions/config.php');
db_connect();
// Executes anything in the { } braces if the button with value="go" is clicked in the form below - if not then it will ignore it
if ($_POST['go']) {
// Assigns the POST item values to variables from the form
$type = $_POST['prop_type'];
$loc = $_POST['location'];
// Checks if variables are not equal to nothing
if (($type !="") && ($loc !="")) {
// I don't think you really need this unless you are doing something with the results from the query
// ---------------------------------------------------------------
$sqlStatement = "SELECT * FROM sitelinks";
$sqlResult = mysql_query($sqlStatement);
if (($type == 'Condominium') && ($loc == 'City1')) {
include('condocity1.php');
}
if (($type == 'Residential' && $loc == 'Province1')) {
include('resprov1.php');
}
}
}
?>
<html>
<head></head>
<body>
<form id="form1" method="post" action="filename.php">
<select name="prop_type" id="prop_type" style="width: 140px;">
<option value="">- Property Type -</option>
<option value="Office">Office</option>
<option value="Residential">Residential</option>
<option value="Condominium">Condominium</option>
<option value="Leisure">Leisure</option>
</select>
<select name="location" id="location" style="width: 140px;">
<option value="">- Location -</option>
<option value="City1">City1</option>
<option value="City2">City2</option>
<option value="Province1">Province1</option>
<option value="Province2">Province2</option>
</select>
<input type="submit" name="go" id="go" value="GO" />
</form>
</body
</html>
sunnychotai
10-10-2008, 04:44 AM
Try google - you will find lots of code samples... any more q's then shout.
:)
I am not sure why you are querying a database. What is in there? And do you want to do anything with the results?
cyberjorge
10-10-2008, 04:59 AM
Hi there,
Thanks for your reply again.
I actually don't know what I am doing honestly. :)
I found those codes after googling, I actually thought I still need to save an index list of my static file locations in MySQL database then query it.
I end up being redundant when in fact I can actually do this all in php.
Thanks a lot for switching the light on for me. I really appreciate it.
Thank you also for mentioning about the form, I just add and add this using dreamweaver. I'm still learning, thanks a lot for people like you who are willing to help.
I will try your code, hopefully will work well my AJAX pages/links.
cyberjorge
10-11-2008, 12:53 AM
Hi again sunnychotai,
Just want to thank you, worked like a charm!
Thank you so much!
Need to fix some problem though as the page I'm calling are anchored from AJAX.
Below codes won't work.
include("javascript:showdiv('contentarea'); ajaxpage('index.html#resprov1.html', 'contentarea');void(0)");
Will just ask this on a proper section, but if anyone there has solution, please let me know.
Thanks again!