Jump to content

Using Ajax with a Multiple Select Box?


Punk Rock Geek

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/181979-using-ajax-with-a-multiple-select-box/
Share on other sites

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.