PDA

View Full Version : How to store multiple selected values from listbox


heartlin
03-23-2008, 10:55 AM
Hi,

I am new to Javascript. I want to store the multi selected listbox values into mysql database. I dont have any script regarding this. Can anyone help me.

Regards
Heartlin

xgretsch
03-24-2008, 08:18 PM
Too long a process for me to give you actual code for the whole lot, but here's the sequence you'll need to go through:

First, you need to extract the values from your listbox, using something like this:

var abox=document.getElementById(comboboxid);
var i;
var opt;
for (i=0;i<abox.length;i++)
{
opt=abox.options[i];
if((opt.selected)&&(opt.value!=0))
{
// Do something to record opt.value in an array or list
.................................
}
}

Next, you will want to bundle your options into the GET parameters of a URL and send them off to the server in the standard AJAX process. A good tutorial is at http://www.w3schools.com/ajax/default.asp - the process is tedious but does actually work fairly smoothly. How you bundle the options together is up to you: the easiest is to make them numbers and simply send them as a comma separated list in a GET parameter, something along the lines of http://www.yoursite.com/cgi-bin/yourscripthandler.php?yourchoicelist=20,25,67. If you have very complex info, you'll need to use a POST command (more tricky).

When you've arrived at the server, it depends on what language you're using. I personally use PHP, in which case you simply retrieve your parameter array using something like:

$yourarrayofchoices=explode(",",$_GET["yourchoicelist]);

Don't forget to protect your site against SQL injection - if you don't know what this means, read the relevant section of the MySQl manual - essentially, you have to make sure that people have actually put numbers into their choices rather than any malevolent code.

Finally, you build SQL statements and execute them using the mysql_connect and mysql_query functions in PHP.

Hope this helps a bit - it's quite an involved process...