I have built this ajax function where i want to update two fields in a database if the user decides to change them, i have this code where i'am printing hotelrooms (school assignment) with their current information, ID, etcetera:
<?php
include('php/connect.php');
$chquery = "SELECT * FROM rooms";
$chresult = mysqli_query($conn, $chquery);
while($chrow = mysqli_fetch_array($chresult)){
echo "<div class='edit_roomRow'>";
echo "<h1> Rum " . $chrow['ID'] . "</h1>";
echo "<form>";
echo "Rumsnummer:<br> <input id='c-roomnumber' type='text' value = '" . $chrow['ID'] . "' placeholder='" . $chrow['ID'] . "'><br>";
echo "Beskrivning:<br> <input id='c-description' type='text' value = '" . $chrow['Description'] . "'placeholder='" . $chrow['Description'] . "'>";
echo "</form>";
echo "<br>";
echo "</div>";
echo "<div class='btn btn-warning change-btn' data-value='". $chrow['ID'] . "'style='float: right; margin-top: 100px;'>Ändra</div>";
echo "<hr/>";
}
mysqli_free_result($chresult);
mysqli_close($conn);
?>
Here nothing is wrong and all values seem correct. Then here's the ajax where i send the info from this form:
<script type="text/javascript">
$(".change-btn").click(function(){
var id = $(this).attr("data-value");
var description = $("#c-description").val();
var roomnumber = $("#c-roomnumber").val();
//Call to ajax
$.ajax({
method:"GET",
url: "php/changepost.php",
data:{ id: id, description: description, roomnumber: roomnumber },
success: function(){
$(this).val("");
$("#c-description").val("");
$("#c-roomnumber").val("");
//Reload specific div with rooms, avoid full page reload
$(".changerooms").load(location.href + " .changerooms");
}
})
})
</script>
To make sure all the values are sent from the PHP (correct ID from the button, description & room number) i alerted them in the end of the ajax file, all values correct.
Then here i am trying to update the DB with the new values:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
include('connect.php');
if(isset($_GET['id'])){
$changequery = "UPDATE rooms SET ID = $_GET['roomnumber'], Description = $_GET['description'] WHERE ID = $_GET['id']";
//Perform $changequery
mysqli_query($conn, $changequery);
}
mysqli_close($conn);
?>
I cant see anything that is off here, but i am kind of new with AJAX mostly, so if you see something that seems wrong, i would be glad with any help i could get.. :)