Jump to content

[SOLVED] Dynamic Drop Down List Not Passing Correct Variable


refiking

Recommended Posts

Ok.  So, I am using a dynamic drop down list using a javascript file and the drop down list works perfectly in and of itself. It's function is to display the options of drop down list 2 based on the selection of the user on drop down list 1.  The problem I am having is when I try to run a db query, it is defining $_POST['SubCat'] as 'Color' instead of the color selected (blue for example).  How can I get this to work?  I'll post both the javasrcipt script and the php script.

 

Here's The Javascript


function fillCategory(){ 
// this function is used to fill the category list on load
addOption(document.drop_list.Category, "Color", "Color", "");
addOption(document.drop_list.Category, "Theme", "Theme", "");
addOption(document.drop_list.Category, "Price", "Price", "");
}

function SelectSubCat(){
// ON selection of category this function will work

removeAllOptions(document.drop_list.SubCat);
addOption(document.drop_list.SubCat, "Select Filter Type", "Select Filter Type", "");

if(document.drop_list.Category.value == 'Color'){
addOption(document.drop_list.SubCat,"Orange", "Orange");
addOption(document.drop_list.SubCat,"Blue", "Blue");
addOption(document.drop_list.SubCat,"Red", "Red");
}
if(document.drop_list.Category.value == 'Theme'){
addOption(document.drop_list.SubCat,"Professional", "Professional");
addOption(document.drop_list.SubCat,"Contemporary", "Contemporary");
addOption(document.drop_list.SubCat,"Elegant", "Elegant", "");
}
if(document.drop_list.Category.value == 'Price'){
addOption(document.drop_list.SubCat,"1", "9 Credits & Below");
addOption(document.drop_list.SubCat,"2", "10 - 19 Credits");
addOption(document.drop_list.SubCat,"3", "20 - 29 Credits");
addOption(document.drop_list.SubCat,"4", "30 Credits & Above");
}

}
////////////////// 

function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=0;i--)
{
	//selectbox.options.remove(i);
	selectbox.remove(i);
}
}


function addOption(selectbox, value, text )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;

selectbox.options.add(optn);
}

 

And here's the php where I am having the trouble:

 

<FORM name="drop_list" action="tester.php" method="POST" >
<TD><input type="hidden" name="filter" value="yes">
<SELECT  NAME="Category" onChange="SelectSubCat();" >
<Option value="">Category</option>
</SELECT> 
</TD><TD><b>Only Show: </b></TD><TD><SELECT id="SubCat" NAME="SubCat">
<Option value="">Select Filter Type</option>
</SELECT></TD><TD><input type="submit" value="Filter Now"></TD></FORM>

Link to comment
Share on other sites

Here's the source code:

 

<FORM name="drop_list" action="tester.php" method="POST" >

<TD><input type="hidden" name="filter" value="yes">

<SELECT  NAME="Category" onChange="SelectSubCat();" >

<Option value="">Category</option>

</SELECT> 

</TD><TD><b>Only Show: </b></TD><TD><SELECT id="SubCat" NAME="SubCat">

<Option value="">Select Filter Type</option>

</SELECT></TD><TD><input type="submit" value="Filter Now"></TD></FORM>

Link to comment
Share on other sites

Not sure I understand, but here goes. Here's the code that stops the page from continuing here:

 

$value = $_POST['SubCat'];
$sl = mysql_query("SELECT * FROM `products` WHERE `$value` = 'yes'")or die(mysql_error());

 

I selected Color in the 1st Drop Down Box and Blue in the second drop down box, so it should render $_POST['subcat'] = blue.  Instead it renders $_POST['subcat'] = color.  As evidenced by the error message:

 

Unknown column 'Color' in 'where clause'

Link to comment
Share on other sites

Sorry I'm not making myself clear...

 

What I mean is a sample of the rendered out put i.e. go to the url in a browser, view source and then cut and paste some of that. I got a sneaky suspicion that the Javascript you are using is not filling in the <option value=""> properly causing your php script to fail.

Link to comment
Share on other sites

Yeah that's what I posted earlier.  That was the source code.

Here it is again, though.

<FORM name="drop_list" action="tester.php" method="POST" >
<TD><input type="hidden" name="filter" value="yes">
<SELECT  NAME="Category" onChange="SelectSubCat();" >
<Option value="">Category</option>
</SELECT> 
</TD><TD><b>Only Show: </b></TD><TD><SELECT id="SubCat" NAME="SubCat">
<Option value="">Select Filter Type</option>
</SELECT></TD><TD><input type="submit" value="Filter Now"></TD></FORM>

Link to comment
Share on other sites

What Slip is asking for is the HTML Source you see when the page is displayed, and you look at the source code from the browser.

 

Something is missing when it's being shown through the web most likely.

 

If that is the HTML Source from IE / Firefox, I'm just scanning the functions now.

 

Link to comment
Share on other sites

Yeah. I know what he's talking about.  I copied this straight from the source code.  I opened the page, clicked view source and copied the section in question.  Then, I cut and pasted it here.  From what I'm gathering from you all's responses is this seems to be more of a javascript code problem.  Am I right in that assumption?

Link to comment
Share on other sites

Ah right I do apologise, you won't actually see the additional output that the JS will add via the broswer view source method...

 

I do though believe it is a problem in the Javascript not setting the <select id or <option value correctly. This has got nothing to do with PHP though...

Link to comment
Share on other sites

I found the problem in the javascript code. Here's what I changed it to:

if(document.drop_list.Category.value == 'Theme'){
addOption(document.drop_list.SubCat,"Professional", "Professional", "Professional");
addOption(document.drop_list.SubCat,"Contemporary", "Contemporary", "Contemporary");
addOption(document.drop_list.SubCat,"Elegant", "Elegant", "Elegant");

 

I needed to add another ,"" to the string.  Worked perfectly after that

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.