I am attempting to do my first AJAX call and what I am trying to do is pretty simple, but my db is not being updated.
All I am trying to do is when I hit the accept button next to a user, for their id to be taken and sent with the new status 'Accepted' and for the status to change from 'Pending' to 'Accepted' for that particular user in my user_requests db table.
Nothing is being changed in the db and the only thing that is happening with the AJAX code is I get my #success message, but for maybe 0.3 of a second and it does not fade out.
Does anyone see what I am doing wrong in my attempt?
<h2>Pending User Requests</h2>
<br />
<div id="success" style="color: red;"></div>
<?php
$con = mysqli_connect("localhost", "root", "", "db");
$run = mysqli_query($con,"SELECT * FROM user_requests ORDER BY id DESC");
$numrows = mysqli_num_rows($run);
if( $numrows ) {
while($row = mysqli_fetch_assoc($run)){
//comment added by php-dev : condition could be set in the query -->
if($row['status'] == "Pending"){
$pending_id = $row['id'];
$pending_user_id = $row['user_id'];
$pending_firstname = $row['firstname'];
$pending_lastname = $row['lastname'];
$pending_username = $row['username'];
?>
<!-- comment added by php-dev : useless form tag -->
<form action="" method="POST" id="status">
<!-- comment added by php-dev : useless input field, no field name -->
<input type='hidden' value='<?php echo $pending_id; ?>' id='pending_id' />
<?php
// comment added by php-dev : comparing string to boolean value true
if ($pending_firstname == true) {
echo "Name - ". $pending_firstname . " " . $pending_lastname . "</br>"
. "Username - ". $pending_username . "</br></br>"
?>
<!-- comment added by php-dev : conditional form closing tag -->
</form>
<button class="approve" type="submit" form="status" name="approve"
value="<?= $pending_id; ?>">
Approve
</button>
<button id="deny" type="submit" form="status" name="deny" value="Denied">
Deny
</button>
<br><br><br>
<?php
// comment added by php-dev : else statement misplaced -->
;} else {
echo "There are no Pending Requests at this time.";
}
}
}
}
?>
My AJAX call...
<script>
$(document).ready(function(){
$('.approve').click(function(){
$.ajax({
url: 'userRequest_approve.php',
data: {
id: $(this).val(), //the value of what you clicked on
//you clicked on it so you know the status might as well hardcode it
status: 'Approved'
},
success: function(data) {
//do something with the data that got returned
// comment added by php-dev : for debug purposes, the #success should show
// the server reponse instead
$('#success').html('User Status Changed!');
//do something with the data that got returned
$('#success').delay(5000).fadeOut(400);
},
type: 'POST'
});
});
});
</script>
My userRequest_approve.php file to insert into db to update the status...
<?php
require_once 'core/init.php';
$term = mysql_escape_string($term); // Attack Prevention
$pending_id = $_POST['id'];
$status = $_POST['approve'];
$con = mysqli_connect("localhost","root","","db");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $con->prepare(
"INSERT INTO user_requests (status, date_responded) VALUES (?, NOW())"
);
if ( false===$stmt ) {
// Check Errors for prepare
die('User Request update prepare() failed: ' . htmlspecialchars($con->error));
}
$stmt->bind_param('s', $status);
// comment added by php-dev : should be false === $stmt->bind_param ...
if ( false===$stmt ) {
// Check errors for binding parameters
die('User Request update bind_param() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->execute();
// comment added by php-dev : should be false === $stmt->execute ...
if ( false===$stmt ) {
die('User Status update execute() failed: ' . htmlspecialchars($stmt->error));
}
?>
If you want to update, you should try this:
$stmt = $con->prepare("UPDATE user_requests SET status=?, date_responded=NOW() WHERE id=?");
$stmt->bind_param('si', $status, $pending_id);
You also need a name attribute on your hidden so it'll be sent:
<input type='hidden' name='id' value='<?php echo $pending_id; ?>' id='pending_id'/>
Original Answer
I only see one issue:
This is the ajax request you're using:
$.ajax({
url: 'userRequest_approve.php',
data: {
id: $(this).val(), //<< id
status: 'Approved' //<< status
},
success: function(data) {
//do something with the data that got returned
$('#success').html('User Status Changed!');
$('#success').delay(5000).fadeOut(400);//do something with the data that got returned
},
type: 'POST'
});
Note that the data you're sending is id
and status
.
However, on the PHP side:
$pending_id = $_POST['id']; //yep
$status = $_POST['approve']; //does it exist?
You should be using
$status = $_POST['status'];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.