user5567987 user5567987 - 8 days ago 6
Javascript Question

onchange update gender javascript

My code doesn't work. When I change value of the

gender
select tag, the table doesn't update.
Address
works fine

$(document).on('blur', '.address', function(){
var id = $(this).data("id4");
var address = $(this).text();
edit_data(id, address, "address");
});

$(document).on('change', '.gender', function(){
var id = $(this).data("id5");
var gender = $(this).value();
edit_data(id, gender, "gender");
});

function edit_data(id, text, column_name) {
$.ajax({
url: "edit.php",
method: "POST",
data: {
id: id,
text: text,
column_name: column_name
},
dataType: "text",
success: function(data) {
alert(data);
}
});
}


edit.php:

<?php
include 'database/connect.php';
$id = $_POST["id"];
$text = $_POST["text"];
$column_name = $_POST["column_name"];
$sql = "UPDATE agents SET ".$column_name."='".$text."' WHERE id='".$id."'";

if (mysqli_query($conn, $sql))
{
echo 'Data Updated';
}
?>


select.php - this is where the datatable comes from

<td class="address" data-id4="'.$row["id"].'" contenteditable>'.$row["address"].'</td>
<td>
<select class="gender" onchange="getval(this)" data-id5="'.$row["id"].'" >
<option value="Male" ' . ( $row["gender"]=='Male' ? 'selected' : '' ) . '>Male</option>
<option value="Female" ' . ( $row["gender"]=='Female' ? 'selected' : '' ) . '>Female</option>
</select>
</td>

Answer

Change var gender = $(this).value(); to var gender = $(this).val();