reza
reza

Reputation: 119

pass id from php while loop to jquery

i have a form that has select boxes. this form has a table and each row has been created by php while loop from database. for example

<select name="1265483" id="1265483" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>


<select name="5894253" id="5894253" class="form-control">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>

the id of each select is that entry's id in database. i want to get those ids to make an onchange function then send that id to ajax.

Upvotes: 1

Views: 1211

Answers (3)

Vijay Porwal
Vijay Porwal

Reputation: 152

Try using the select class name:

$("select.form-control").change(function (e) {
    var selectId = $(this).attr('id'),
    ajaxUrl = "demo_test.php?id=" + selectId;
    $.ajax({
        url: ajaxUrl,
        success: function (result) {
            if (result) {
                //success code 
            }
        }
    });
});

Upvotes: 0

dp4solve
dp4solve

Reputation: 411

you can do by below code

      var your_selected_value = $('#5894253 option:selected').val();

            $.ajax({
              type: "POST",
              url: "your_url",
              data: {selected: your_selected_value},
              success: function(data) {
                // Stuff
              },
              error: function(data) {
                // Stuff
              }
            });

put this code in onchange function

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

You can use something like:

$("select").change(function () {
    $this = $(this);
    $.post("some/url/dot.php", {data: $this.attr("id")}, function () {
        // code...
    });
});

Upvotes: 2

Related Questions