Page 1 of 1
List Box population using HTML_AJAX and Mysql ?
Posted: Mon Mar 12, 2007 4:14 am
by aniltc
hi
i am using HTML_AJAX pear class for ajax related functions.I would like to implement listbox population (using mysql) using HTML_AJAX Pear class
i am not getting an idea how to do this?i tried to get the answer from PEAR.But ,i am not able to do.If anyone done this using HTML_AJAX,please help me and what r the functions i should use for doing list box population (list box population means-clicking in the one selected select box will populate the next select box available in the page based on the first one -ie chained select box)
please help me
thanks to all
Posted: Mon Mar 12, 2007 4:56 am
by CoderGoblin
Not sure on HTML_AJAX pear. We do have an example of chained select using two of the more common javascript libraries out there (prototype and jquery) at
Dynamic/Chained Selects using Ajax Prototype/JQuery.
If this doesn't help it would be a good idea to post some of your code for us to look at.
Posted: Mon Mar 12, 2007 5:16 am
by aniltc
Thanks
It is based on arrays.Can You please give me an idea How can i Implement same using Mysql
Thanks once again
Posted: Mon Mar 12, 2007 5:56 am
by CoderGoblin
Should be fairly simple... A possibility is shown below although I have to admit I do not use mysql myself so there may be errors.
sublist.php
Code: Select all
<?php
// Set up connection to mysql:
$dbconn = mysql_connect('database', 'dbuser', 'dbpassword');
if (!$dbconn) {
die('<span style="color:red">Error accessing database</span>'); // If we have an error.... second select is replaced by a span
}
// Process
$value=1; // set the default option
if (!empty($_POST['first'])) {
if (isset($sublist[$_POST['first']])) $value=$_POST['first']; // override default option
}
$name_list='';
// use a mysql query to get all information based on the value passed in....
$query = sprintf("SELECT value,text FROM optiontable WHERE parent='%s'",
mysql_real_escape_string($value));
// Perform Query
$result = mysql_query($query);
if ((!$result) OR (mysql_num_rows($result)==0)) {
echo '<span style="color:red">Error: Option not available</span>' // If we have an error.... second select is replaced by a span
} else {
while ($row = mysql_fetch_assoc($result)) { // build the options
$name_list.="<option value=\"{$row['value']}\">{$row['text']}</option>\n";
}
echo '<select name="second">'.$name_list.'</select>'; // output
}
mysql_close($link);
?>
You would probably do something similar for the first select in index.php.
Posted: Mon Mar 12, 2007 6:18 am
by aniltc
Thanks a lot
suppose i have 6 drop down boxes on a single pages.How can i retain all the list box values?
Can i use any prototypes technique for retainig values or should i select again from the database?
please clarify my doubts
Thanks once again
Posted: Mon Mar 12, 2007 6:45 am
by CoderGoblin
Multiple checkboxes should be fine although you will need to come up with a naming scheme to work things out and adjust code accordingly. If all the options have the same query things will be easy. If not you may need different parameters passed in addition to the example. You may very well need to pass additional values through ajax if you want to retain selection. I recommend you get one selection working first and try to understand what is happening. Then gradually build up. You may also like to switch off javascript and try the code. There is a change to the initial examples within the previous link topic explaining how to handle this gracefully.