Punk Rock Geek Posted November 18, 2009 Share Posted November 18, 2009 I have a form containing a multiple select box, as follows: <form action="" method="post" enctype="multipart/form-data" name="admin"> <select name="selectname" id="selectid" size="6" multiple> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <input name="submit" type="button" value="Submit" onclick="my_javascript(1)"/> </form> Please assume that I already have knowledge of how basic AJAX is structured. I only need to know how the above particular example will differ from basic ones. (For example, if I have an empty name field, and I fill in the name and press submit, my code already successfully adds that name to the database and shows the new name on the page without a refresh.) I am not able to replicate this with multiple options at once however. Specifically, how can I get every option that is selected, to be transferred to the "my_javascript" function? Furthermore, after I send those options from the javascript to the PHP (I assume this step will be identical to "basic" AJAX implementations?), then how do I add each option to the database in my PHP file? Thanks! Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 18, 2009 Share Posted November 18, 2009 try this out <script> function get_selected(id) { arr_selected = new Array(); obj_select = document.getElementById(id); for (i=0; i < obj_select.length; i++) { if (obj_select[i].selected) { arr_selected.push(obj_select[i].value); } } return arr_selected.join('|'); } </script> <form action="" method="post" enctype="multipart/form-data" name="admin"> <select name="selectname" id="selectid" size="6" multiple> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <input name="submit" type="button" value="Submit" onclick="alert(get_selected('selectid'))"/> </form> Quote Link to comment Share on other sites More sharing options...
Punk Rock Geek Posted November 20, 2009 Author Share Posted November 20, 2009 Awesome, thanks! It worked! What would I need to change if I wanted every option in the select box (NOT just the options selected) to be returned in the javascript? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 20, 2009 Share Posted November 20, 2009 just remove the if statement checking if its selected or not Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.