![]() |
| ||
| AJAX generated <select> and FIREFOX Hello, I am trying to implement two cascading <select> boxes. the second box's options would be fetched from a mysql db via a PHP script. it works fine on IE. Problems rise when it comes to Firefox, the form would not submit the value from the second <select> box. I have the feeling that we need to return the name/value pairs of data and generate the <option>s on client-side, I just dont know how to do it using DOM. Please advise.. Below is code for both the JS and PHP scripts: PHP script
Javascript
|
| ||
| Re: AJAX generated <select> and FIREFOX help pls! |
| ||
| Re: AJAX generated <select> and FIREFOX It looks like you are creating a select element with unclosed options elements. You have the opening and closing tags for select, but I only see the opening tags for the options, which may or may not be your problem. I use dynamically created options for my select's using ajax requests to PHP code that gets data from a database as well, but the route I took was to have the select elements present in the HTML code with one dummy <option></option>. When my ajax request is made, my php does not generate text to get with responseText, but it creates an XML document that has the values+text for each element and retrieved with XMLresult instead of responseText. For my company contacts menu their names are the text and the value is their email address and the XML doc would contain: <row>Bob,bob@abc.com</row> Of course this is not a full XML doc, but my JS iterates over the "row" tags in the XML and puts the info into a global array named company_email. Then I use the following to remove any/all options from the select object, and create new options, split the company_email at the comma and set the value and text of the option elements accordingly. var cs = document.getElementById("<your select element id here");
I just found it easier to work with elements that were generated in the HTML document than to try to dynamically create new elements on the fly, if you want to try this different route and would like to see how I create and read the XML docs let me know. |
| ||
| Re: AJAX generated <select> and FIREFOX Hello HazardTW, thank you for your reply. I would very much like to know more about your approach. |
| ||
| Re: AJAX generated <select> and FIREFOX Try this out... You need a table in your DB named email with at least one column named email. The data in each row should be in this format: name,email Example: Bob,bob@bob.com I did test this and verified that it works as long as: a) you can connect to your database b) you have a table named email with a column name email c) you can run PHP on your host server I assume The php file outputs an XML document, you can just open the PHP directly in the browser and see it's output, might help you get an idea of what it's doing. The PHP file:(test.ajax.php) <?phpAnd the HTML with javascript in it: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> My disclaimer is that I have never been trained in programming, only self taught, so any pro's out there that see anything they wish to comment on, I would be happy to receive the critique.(with the exception of the HTML portion of the doc above that I just threw together for this example) ;) For simplicity I have been retrieving all rows I get from my database this way, in a "^" delimited string that I split once I have retrieved it. Hope this helps you with your menu building. This method has worked great so far, my goal for the app I am working on is to never have to reload the page and the menus are updated every time the layer is they are on is made visible. |
| ||
| Re: AJAX generated <select> and FIREFOX Interesting approach you've got here.. appreciate your time. I am no programmer myself, just trying to work things out :/ I tried your example and it worked fine. My requirements are a bit different though, I have two <select> boxes. first contains a list of countries second would a list of the cities in the selected country (i.e will be changed each time the first select is changed) any ideas? P.S: i found out that closing the <option> is optional in most of the browsers. did close it anyways though. |
| ||
| Re: AJAX generated <select> and FIREFOX Browsers will go into quirks mode when encountering markup errors, they try to correct your errors which can from my understanding result in slower pages and pages that do not render quite right. Again from my understanding it is best to use a strict doc type and validate your code, the strict doc type tells the browser your doc contains no errors and to render it exactly as you have written. On your cascading selects, that is actually more simple than you might think, I have them implemented 3-deep in my current app. Here is an example of 2-deep cascading selects for country and city. When you change the value of country, the "onChange" calls the function to rebuild the city menu, sending it's value for the function to use. You will of course need to have all the arrays filled, you should be able to use the previous examples to fill in global variables for countries and cities. Example: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> You might use one global variable for the cities menu, and add an ajax call from the build_city_menu function requesting the cities from your database for the country name passed to the function, have the returned result a comma delimited string place right into a global city_list and replace cities[] with that. That would save you having to fill city lists for every country. |
| ||
| Re: AJAX generated <select> and FIREFOX Just remember that the validator can't validate code created by JavaScript. If an error occurs in a strict doctype due to JavaScript changing the code, the page often will quit working at that point. |
| ||
| Re: AJAX generated <select> and FIREFOX point taken regarding strict doc type. spot on. Quote:
In my first post I managed to populate the second select box just fine. the whole problem was in the Ajax call. I couldnt manage to write a proper ajax method that would work on all browsers (firefox didnt like my method) (refer to my first post) it would be nice to see your way of doing it. cheers mate. |
| ||
| Re: AJAX generated <select> and FIREFOX I will post more when I get home, but wanted to say thanks for that comment MidiMagic, I had not thought about that, so far I have had no problems, keeping fingers crossed. Regarding Firefox not liking your HTTP requests, I have not figured why that is, but installing Firebug has worked on 3 PC's with Firefox 2.0.0.6 installed. Each would not run my app until I installed Firebug, just google it, easy to find and install. What's funny is I initially installed Firebug so I could view the response headers to see what was going on, but it magically started working then ;) EDIT: btw MidiMagic, are you referring to html created via document.write(), .innerHTML, or .createElement() + .appendChild()?? |
| All times are GMT -4. The time now is 2:37 am. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC