On selection of the first list box my page is not reloading
Page reloading is done by JavaScript function. This function is triggered by onchange event of the first drop down box. So check that you have written the trigger correctly and the function is there. Inside the function you have written the code correctly and the line self.location=dd.php?cat' + val ; is correctly replaced with your page name and variable name. As we are using JavaScript it should be enabled in your browser. Watch your browser address bar and it should read the page name with the value of the selection box name cat.
This value of cat is available now for our second drop down list box to use after the page reloads. Here to collect the value of cat from address bar we have to use like this.
$cat=$_GET['cat'];
Now value of $cat is available and it is a good idea to test the value of $cat by giving a print command to check the value. Now once the value of cat is available it should go to the second drop down box query. If you have doubt then print the second query to screen and see how it is constructed. It is always a good idea to print the MySQL error message to screen after the mysql_query() command.
I want to include this along with my other form field components.
Just ensure that all the other form components are within your form end tag. But here as the page is reloading we have to take care to retain the values of the other form components also. If the drop downs are the first to be filled then we can afford to keep other fields blank, but here if some one has already filed the lower part of the form first and the page reloads then the entered data for the other form components are lost. So inside the javascript form which triggers the page reloads we can keep all other form component values and pass them like category value back to the form.
If you are submitting the form with drop down list boxes to another page and using another form with other details to be filled then use hidden tags to keep the drop down selected values in second form.
Can I use more than two drop downs and link them ?
Yes this can be done. But first start with two drop downs and understands how they are linked and values are taken from one drop down to other. Once the page reloads the values are to be taken through query string ( at address bar ) so they are to be collected and formatted with the page name inside the JavaScript function. You can see the three drop down tutorial and demo. After a good understanding of this you can extend to more number of drop downs.
← Subscribe to our Youtube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com