I have mysql database. On my page, when I click on a button I retrive data from database. All data are displayed on page. I get one row with id, one row with name and one row with number (in input field). So one example is that I get:
51
Table
200
52
Head
320
53
Glass
456
Because all value number (3rd row) are in input fields they can be changed. I created submit button so I can submit changed values. Here is my js code:
$(document).ready(function () {
$("#submit2").click(function () {
event.preventDefault();
$('form#list_prices_form input[type=number]').each(function () {
default = $("#default").val();
var value = $(this).val();
var id = $(this).attr("name");
console.log(id);
console.log(value);
});
$.ajax({
type: "POST",
url: "save.php",
data: {default: default, value: value, id: id},
success: function (response) {
default = $("#default").val();
$.ajax({
type: "POST",
url: "retrive.php",
data: {default: default},
success: function (response) {
$('#list').html(response);
}
});
}
});
return false;
});
});
So in console I get all necessary fields. They show correct id and value for each field. But it shows error in console:Uncaught ReferenceError: value is not defined
And php code:
$default = $_POST['default'];
$value = $_REQUEST['value'];
$id = $_REQUEST['id'];
$result = $conn->query("SELECT * FROM tbl_name WHERE default = '$default'";
$query = "UPDATE tbl_name SET (value_row) VALUES(?) WHERE id='$id'";
$statement = $conn->prepare($query);
$statement->bind_param('i', $value);
if ($statement->execute()) {
echo 'Changed';
//print 'Success! ID of last inserted record is : ' . $statement->insert_id . '<br />';
} else {
die('Error : (' . $conn->errno . ') ' . $conn->error);
}
$statement->close();
.each. Instead of multiple ajax I would suggest to pass an array. And at server side read that array and haveforloop to iterate over incoming array.