PDA

View Full Version : drop down combos using $_GET not working as it should


nanny
12-12-2007, 05:59 AM
Hi I use Davidj's combo box on my forms and it works well. However I am using it for my search and it won't submit automatically like it does for $_POST.
Here is what I am using:
$country = $_GET['inv_country'];

The select statement is:
"SELECT * FROM inv_state INNER JOIN country ON inv_state.inv_country = country.inv_country AND country.inv_country = '$country' "
This is the drop down for country:
<select name="inv_country" onchange="document.forms[0].submit()">
<option value="" <?php if (!(strcmp("", $_GET['inv_country']))) {echo "SELECTED";} ?>>Select Country</option>
<?php
do {
?>
<option value="<?php echo $row_RScountry['inv_country']?>"<?php if (!(strcmp($row_RScountry['inv_country'], $_GET['inv_country']))) {echo "SELECTED";} ?>><?php echo $row_RScountry['inv_country']?></option>
<?php
} while ($row_RScountry = mysql_fetch_assoc($RScountry));
$rows = mysql_num_rows($RScountry);
if($rows > 0) {
mysql_data_seek($RScountry, 0);
$row_RScountry = mysql_fetch_assoc($RScountry);
}
?>
</select>
<?php if (isset($error['inv_country'])) { ?>
<span class="errmsg1"><?php echo $error['inv_country']; ?></span>
<?php } ?>

My state select is:
<select name="inv_state" id="inv_state">
<option value="" <?php if (!(strcmp("", $_GET['inv_state']))) {echo "SELECTED";} ?>>Select Region</option>
<?php
do {
?>
<option value="<?php echo $row_invstate['inv_state']?>"<?php if (!(strcmp($row_invstate['inv_state'], $_GET['inv_state']))) {echo "SELECTED";} ?>><?php echo $row_invstate['inv_state']?></option>
<?php
} while ($row_invstate = mysql_fetch_assoc($invstate));
$rows = mysql_num_rows($invstate);
if($rows > 0) {
mysql_data_seek($invstate, 0);
$row_invstate = mysql_fetch_assoc($invstate);
}
?>
</select>
<?php if (isset($error['inv_state'])) { ?>
<span class="errmsg1"><?php echo $error['inv_state']; ?></span>
<?php } ?>

Just not getting it to show the inv_state unless the form is submitted.

What am I doing wrong?

davidj
12-12-2007, 06:16 AM
how many forms do you have on the page??

nanny
12-13-2007, 07:16 AM
only the one - this one with 4 select menus one for industry one for category one for country one to connect with country for state/region and a text box.

davidj
12-13-2007, 08:15 AM
you have 2 selects (dropdowns)

your first is populating your second yes ???

nanny
01-01-2008, 11:24 PM
Hi sorry been away for a while. Well I tried the $_GET and found the $_POST most successful with error checking if the select list is left empty to have the selection resubmitted without the user having to re-do the country select over again. With $_GET the user had to select again if there were errors with the whole form.
What I did was have different location forms for searching the advanced e.g. one for Australia, UK, NZ, US/Canada and that works well.
I also use the $_POST dropdowns for my listing submissions and it works well.
The only question I have is how to make the second select list go to the next dropdown instead of the top of the page where I have multiple text boxes first before the dropdowns.
I also played with the ajax dropdown tutorial and it works well for one instance but the same applys if a person makes an error then the user has to re-select the country again and it takes more time than the first dropdown tutorial you had.
Great tutorials by the way.

But I am needing to have the first dropdown select to re-submit to the next dropdown. The selection shows but the user is taken to the top of the page.

Cheers - happy new year.......

nanny
01-16-2008, 10:31 PM
Hi still having trouble with this one needing some help........ I need this for my advanced search where country and state are dependant on each other but have two other dynamic drop downs and a text box.
All I get in results is all or nothing........ any ideas I have been searching for help with this one for about 6 months and nowhere has a solution.
Cheers

davidj
01-16-2008, 10:43 PM
so you want the page to stay in place when selecting the dropdown?

and you want the value of the first dropdown to remain rather than resetting

nanny
01-18-2008, 01:51 AM
Yes that as well as the results page will not give the proper results for this search form even if all is selected.
I either get all results in the database or it is all wrong. You see if I choose to search from the dropdowns hairdressing, salons, Sydney Australia the results will show all categories and from New Zealand or the UK etc.
Even if I select one dropdown the result is wrong.
Hope that makes sense.

I am not inserting data I am searching data.

davidj
01-18-2008, 05:46 AM
your using 3 dropdowns in your example

is this correct

nanny
01-18-2008, 07:43 AM
Actually it is 4
1: Category
2: Subcategory
3: Country
4: Location/City (this one is dependant of the Country dynamic dropdown)

davidj
01-18-2008, 09:34 AM
are you passing each value back

dropdown 1 = $var1 passed
dropdown 2 = $var1 & $var2 passed
dropdown 3 = $var1 & $var2 & $var3 passed

so every time you move to the next you have to pass the previous vars aswell to retain the query

nanny
01-19-2008, 10:09 PM
I think I might have to go through this with you - I have tried so many methods and getting confused.

What I have done previously is:
Assign a variable to each one e.g.

$Category = $_GET['Category'];
$Type = $_GET['Type'];
$country = $_GET['inv_country'];
$state = $_GET['inv_state'];

The inv_country and the inv_state are the dependant drop downs and the mysql is:

SELECT * FROM inv_state INNER JOIN country ON inv_state.inv_country = country.inv_country AND country.inv_country = '$country'


$country = $_GET['inv_country'];

Then when the submit button is pressed I have used if empty give an error message to make them select all.
I have tried just having a keyword textbox as well in case they want to include that for advanced and I don't use the if empty on that text box.

This is my mysql:

SELECT listingf.listingfID, listingf.classcatid, listingf.categorytype, listingf.classbusinessname, listingf.tel, listingf.contactphone2, listingf.contactfax, listingf.inv_state, listingf.inv_addr1, listingf.inv_addr2, listingf.inv_zip, listingf.inv_country, listingf.status FROM listingf INNER JOIN class_category ON class_category.classcatid = listingf.classcatid INNER JOIN categorytype ON categorytype.categorytype = listingf.categorytype INNER JOIN inv_state ON inv_state.inv_state = listingf.inv_state INNER JOIN country ON country.inv_country = listingf.inv_country WHERE listingf.status = 'notactive' AND ( listingf.inv_country = 'inv_country' AND listingf.classcatid LIKE 'Category%' AND listingf.categorytype LIKE 'Type%') ORDER BY listingf.classcatid ASC


Pretty huge hey but I have innerjoined the dynamic dropdowns and the Category and Type are echoed onto the page to show the results the user selected.

davidj
01-19-2008, 10:16 PM
your going to have to skype me with this one

nanny
01-19-2008, 10:35 PM
Sorry I don't have Skype

davidj
01-19-2008, 11:36 PM
think about how it works

your using multi dropdowns which are caught by a sql statement

the first dropdown is caught which generates content for the second dropdown

when selecting the second your aiming to populate the 3rd but you have to remember to also pass in the previous choices again by resubmiting the forms or adding and building up the query string when using $_GET's as you move along the choices

nanny
01-19-2008, 11:56 PM
OK so I would need to do something like this:

if(($_GET['Category'] && $_GET['Type'] && $_GET['inv_state'] && $_GET['inv_country] || $_GET['inv_addr2'])!="");
header('Location: result.php');
exit;
}
else
{
//not sure what to put here...
}

davidj
01-20-2008, 11:05 AM
that is just checking conditionally if something is true

ok

lets look at this...

your first choice is selected and you $_POST the form
this populates the second dropdown then selecting this ->
populates the thirdyou can use a strcmp function in your previous options to remember what they are set to therefore when the page is submitted again the value in the previous dropdown has been remembered in order to be submitted next time round


<option value="monday" <?php if(!(strcmp($_POST['fieldname'], "monday"))){echo "selected";} ?>>Monday </option>

nanny
01-20-2008, 10:17 PM
That is what I have been doing in a sense - but can you mix up $_POST and $_GET in a form.
What I mean is the form is using the get method but to populate the dropdowns can I use $_POST as that works?
But in the conditional for error checking would you use $_POST event though if your are checking submit with $_GET e.g.

$country = $_POST['inv_country'];

SELECT * FROM inv_state INNER JOIN country ON inv_state.inv_country = country.inv_country AND country.inv_country = '$country'

This is the state dropdown dependant of the country dropdown. It works well with $_POST

if (array_key_exists('Go', $_GET)) { //the submit button name and method
$Category = trim($_POST['Category']);
if (empty($Category)) {
$error['Category'] = 'Select Industry';
}
$Type = trim($_POST['Type']);
if (empty($Type)) {
$error['Type'] = 'Select Category';
}
if (!isset($error)) {
header('Location: result.php');
exit;
$_GET = array();
}
}


This what I mean with mixing it up. I haven't put all in here.

davidj
01-20-2008, 10:21 PM
if you $_POST it is sent through the headers on request

it you $_GET it is passed through the address field

$_GET's are limited by string length and will be chopped if exceeded

$_POST's are not limited and you dont get to see the passed values

it is ok to pass a reference through the $_GET method like a record ID but you should not pass form values really

nanny
01-20-2008, 10:35 PM
So I should change all parts of the form and the error checking to $_POST even though it is a search form?
Will I need to change my mysql to $_POST as well?

SELECT listingf.listingfID, listingf.classcatid, listingf.categorytype, listingf.classbusinessname, listingf.tel, listingf.contactphone2, listingf.contactfax, listingf.inv_state, listingf.inv_addr1, listingf.inv_addr2, listingf.inv_zip, listingf.inv_country, listingf.status FROM listingf INNER JOIN class_category ON class_category.classcatid = listingf.classcatid INNER JOIN categorytype ON categorytype.categorytype = listingf.categorytype INNER JOIN inv_state ON inv_state.inv_state = listingf.inv_state INNER JOIN country ON country.inv_country = listingf.inv_country WHERE listingf.status = 'notactive' AND ( listingf.inv_country = 'inv_country' AND listingf.classcatid LIKE 'Category%' AND listingf.categorytype LIKE 'Type%') ORDER BY listingf.classcatid ASC

To how I use the conditional?
Hope that makes sense... what I am getting at is I have used all $_GET method and the result wouldn't work for the dropdowns on error submit.

davidj
01-20-2008, 10:40 PM
you are going snow blind

what i mean is that your stabbing blindly in the dark hoping you will get it to work by changing this and that. You are using some advanced techniques as well so I'm confused why your having trouble with this

all i can suggest is you install skype and i can help you from there

nanny
01-20-2008, 10:49 PM
Thanks davidj - I totally understand what I am doing with this but the search result doesn't work any which way I try. This is why I am stabbing blindly in the dark. Anything that should make sense doesn't. I have a keyword search box - no trouble there - I have a collection of categories selections on the page e.g. hairdressing and under that category you select business resources it works perfectly you only get the results of hairdressing and business resources.
Now the difference with this is not the mysql and the result page is the same
But when I try to use $_GET for this form everything goes haywire with the dropdown dependant form it only likes $_POST and yes it is weird.

davidj
01-20-2008, 11:53 PM
no its not weird

weird is where you code some code then look out of the window and see a clown painted blue holding a dwarf under his arm... now thats weird

what your getting there is a misunderstanding.

you not grasping the technology or the method you using and are just printing it out symbol by symbol as it states in the tutorial or book your using. Like making Chinese sounding words but not understanding the language

OK lets start again

what are you trying to do here

nanny
01-21-2008, 12:46 AM
OK I got it working by using a separate form for each country and got rid of the inv_state out of the variables for searching.
e.g.

SELECT * FROM inv_state INNER JOIN country ON inv_state.inv_country = country.inv_country AND country.inv_country = 'New Zealand';

When I was doing the dependant dropdown for country - state I was using this:

SELECT * FROM inv_state INNER JOIN country ON inv_state.inv_country = country.inv_country AND country.inv_country = '$country'
$country = $_POST['inv_country'];

and on the country form: onchange="document.forms[0].submit()"

Which works well for everything being $_POST
Now because the variables work for $_GET for the search form - it wouldn't work especially on page refreshing etc.

But now that I have removed the inv_state and inv_country from the variables and it works - not sure if the dropdown will work.
I tried looking up in google for the onchange="document.forms[0].submit()"
using it with $_GET but I had no luck seems it is used only with $_POST.

I may well have to have the user searching location via country.
I can see why the inv_state is joining via the inv.country now I want to see if I can use the country.inv_country = '$country' with a dropdown for it.

nanny
01-21-2008, 01:57 AM
Actually I changed my select statement to have AND instead of OR and now I can place the inv_state and the city one in there and it works.
I thought using AND you would need to select all in the form but it shows you don't have to.
Need to catch up on some sql info.

davidj
01-21-2008, 06:29 AM
have you really sorted it?

thats great

or are you just skype shy

nanny
01-21-2008, 09:51 PM
Well I had a sleep and tried it again. It works well if I select nothing it will give all results for that country. If I select a couple it gives the selection for that, if I only select one it gives the result for that within that country and if I place a city name in the text box it works for that as well.
The only thing the text box for city name is only allowing you to place the correct spelling of the name.

Have you ever used fulltext searching?

I thought I should try it for the search. I thought it might be useful for my keyword search box I have on every page because if you use a comma or a hyphen etc. the result doesn't show up as it does if you don't put them in the text box. For example if a user has placed a listing with make-up and you search for makeup it won't give you a result and visa versa.

nanny
01-21-2008, 11:30 PM
Actually I have read on some webpages and they suggest to only use fulltext searching if you have a large site it won't return enough results if you only have a small/medium sized website. As this is starting up maybe it isn't a solution.
Anybody have any ideas?