I'm having a peculiar problem with this app. It doesn't update the columns always and there is no rule as to when it will be updated and when will it not. Seems totally random, and doesn't get me back to admin.php page.
Additionally, when it does change/update the entry in the db it doesn't send a response to Ajax (alert('OK')). Code below:
--this is admin.php
<div class="update" style="display:none">
<form method="post" action="adminFunctions.php">
<div class="input-group">
<label>new name</label>
<input class='newNameInput' type="text" name="newName" >
</div>
<div class="input-group">
<label>new last name</label>
<input class='newLastNameInput' type="text" name="newLastName">
</div>
<div class="input-group">
<button type="submit" class="btn changePerson" name="changePerson">change</button>
</div>
</form>
</div>
then Jquery:
$(document).ready(function(){
var changeId;
$('.changeMe').click(function () {
var el = this;
var id = this.id;
var splitid = id.split("_");
changeId = splitid[1];
$('.update').css('display', 'block');
});
$('.changePerson').click(function () {
var newNameFor = $('.newNameInput').val();
var newLastNameFor = $('.newLastNameInput').val();
$.ajax({
url: './adminFunctions.php',
type: 'POST',
data: 'changeSth=' + changeId + '&newName=' + newNameFor + '&newLastName=' + newLastNameFor,
success: function(response) {
if(response == 1) {
alert('OK');
} else {
console.log('entry update failed');
}
}
})
});
});
--and adminFunctions.php
if(isset($_POST['changeSth'])) {
$id = $_POST['changeSth'];
$newName = $_POST['newName'];
$newLastName = $_POST['newLastName'];
changeEntry($id, $newName, $newLastName);
}
function changeEntry ($id, $newName, $newLastName) {
global $testConn;
if($id) {
$query = "UPDATE people SET firstName='$newName', lastName='$newLastName' WHERE id=$id";
mysqli_query($testConn, $query);
header('location: admin.php');
echo 1;
} else {
echo 0;
}
header('location: admin.php');
}
Thanks in advance!
</div>