I need to make a dynamic dropdown ( select box) that grabs some categories I am storing in the database. The problem is once the user selects the category I need to run a query that selects all the sub categories underneath that category and keeps on populating till there is no more sub-sub categories to show up.
I would definately say use ajax. I had to do something similar for my degree and used ajax for this. I would recommend breaking down w3schools' suggest example and working tweaking it for your use. If you're still stuck PM me and I'll send you my source code.
HERE IS WHAT I DID:
I tried axjax but I was using zencart and can't have spaces in the axjax in the header and if I would have taken out all the space would have taken me forever in the zencart files. I am open to any suggestion to make this better, but its just a modified version from w3schools.
IN your main file put call the javscript showUser function on change in the select box
// make a connection to the database here
//$sql="select c.categories_id, c.parent_id, p.categories_name from categories c JOIN
// categories_description p ON (c.categories_id = p.categories_id) WHERE c.parent_id =
// any(select c.categories_id from categories_description c where c.categories_name = ".(int)$_GET['cid'] . ")";
$sql="select c.categories_id, c.parent_id, p.categories_name from categories c
INNER JOIN categories_description p ON (c.categories_id = p.categories_id)
WHERE c.parent_id = ".(int)$_GET['cid'];
echo '<div id ="dropdownCat'.($q+1).'"sub">';
<b>Customer info will be listed here.</b>
var id_cnt = 0;
alert ("Browser does not support HTTP Request")
id_cnt = id+1;
url=url+"?q="+(id+1) + "&cid=" + cat_id
Why not write the page so that is submits to itself when the on change happens and have the 2nd query run and populate a second drop down field and then display the page again with all its current selections, this would require some scripting to do this but it means that your only using PHP and a database and not relying on several technologies on server and client side.
I can see where the guys are coming from when they say use AJAX but this can have some working problems as you seem to be finding out.
Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08) Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08) I want to leave this world the same way I came into it, Screaming, Incontinent & No memory! I laughed that hard I burst my colostomy bag... (\\.\ May03) Life for some is like a car accident... Mine is like a motorway pile up... Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?
Thanks for suggestion. I did think about it, but I wanted to use ajax(learn something new) and an assumption that going with that approach will take longer to run on the page instead of using AJAX(as its reloading the page again once someone clicks on the drop down, I may be wrong with that though but thats what I have noticed)
Also, it may be misleading in my previous post. I was able to fix this issue with ajax, Before that I tried using axjax but, after I installed axjax and fixed all the bugs I noticed I couldn't output a space or anything before I called axjax on the page itself. Now thats a no-go since it was a zen cart project and that uses multiple files before even loading the page itself.
So finally tried ajax and used this script. Thanks to all guys who helped me at work and at webdeveloper to learn something new. I am surprised there is not a forum for ajax itself though at WD.