I have dynamic table, in that table every row has a dynamic dropdown list. So every dynamic dropdown list have a unique id, I have to pass this id to the onChange function. And from this onChange function I have get the value of selected dropdown list. Please see this question for more info.
ByDefault every job is New Job. If I change the dropdrown list from new Job to close job, then I have to change the status of job new to close job in data base and refresh table, that row will be not in table which change from new to close job, So How I will do using even delegation
I am able to get the id of dropdown list But when I use var value=$('#id').va; It gives undefined. So how to get the value of selected drop down list.
,
Java Script
<script>
function fetchData1(){
$(".data-contacts1-js tbody").empty();
$.get("http://localhost/service/Services.php", function(data) {
var obj=JSON.parse(data);
for(var i in data){
var tr=$("<tr></tr>");
tr.append(
"<td>" + obj[i].b_id + "</td>" +
"<td>" + obj[i].cust_name + "</td>" +
"<td>" + obj[i].cust_mobile + "</td>" +
"<td>" + obj[i].cust_email + "</td>");
tr.append(
"<select id="+obj[i].b_id+" class='input-small'><option value="+1+">New Job</option><option value="+2+">WIP Job</option><option value="+3+">Close Job</option></select>");
$(".data-contacts1-js tbody").append(tr);
i++;
}
});
}
$(document).ready(function(){
$(".data-contacts1-js tbody").empty();
$('.data-contacts1-js').on('change', '.input-small', function(){
update(this);
});
$('#fetchContacts1').click(function() {
fetchData1();
});
});
function update(el){
$.ajax({
type:"POST",
url:"http://localhost/service4homes/updateBooking.php",
data: { status: $(el).val(), id: $(el).attr('id')},
success: function(response){
alert("success");
},
error: function(){
alert("fail");
}
})
};
</script>
HTML Code
<div class="row-fluid">
<div class="block">
<div class="navbar navbar-inner block-header">
<div class="muted pull-left">Carpenter Services</div>
</div>
<div class="block-content collapse in">
<div class="span12">
<table class="data-contacts1-js table table-striped" >
<thead>
<tr>
<th>ID</th>
<th>Customer Name</th>
<th>Customer Mobile</th>
<th>Customer Email</th>
<th>Status</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<button id="fetchContacts1" class="btn btn-default" type="submit">Refresh</button>
</div>
</div>
<!-- /block -->
</div>
Server code
<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","1245");
mysql_select_db("services");
$response = array();
if(isset($_POST['type']))
{
$status = $_POST ['status'];
$id=$_POST ['id'];
$result = mysql_query("UPDATE booking SET sps_id = '$status' WHERE b_id = $id");
if($result){
$response["success"]=1;
$response["message"]="Product successfully updated.";
echo json_encode($response);
} else{
$response["success"]=0;
$response["message"]="Product unsuccessfully updated.";
echo json_encode($response);
}
}
else{
$response["success"]=0;
$response["message"]="Required fields is missing.";
echo json_encode($response);
}
?>