nTuply
nTuply

Reputation: 1374

How to insert dropdown value into database using ajax?

I have a dropdown menu (Select) that loads a bunch of data form my table. I'm trying to insert each value into another table on clicking each selection but I have no clue on how to go about this. I've been looking around and there is nothing much I can find.

Here's my dropdown code:

<select id="theSelect">
    <option value="select">Select all that apply.</option>
<?php
    for($i=0; $i<count($preferences); $i++){
?>
    <option value="<?php echo $preferences[$i]['pname'];?>"><?php echo $preferences[$i]['pdesc'];?></option>
<?php
    }
?>
</select>

What I want to do is each time I select a value from the dropdown, it makes an ajax request that inserts that value into another table. Is that possible?

I would really appreciate some help with that.

Thanks.

Upvotes: 1

Views: 1200

Answers (1)

Voque
Voque

Reputation: 168

Are you looking for something like this?

    $("#theSelect").on('change', function () {
        var value = $('#theSelect option:selected').text();
        $.post("urlToActionDbSave", { valueToInsert: value }, function () {
            alert("your value have been saved to another table")
        })
    })

Upvotes: 2

Related Questions