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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.