After much struggling and reading online I still cannot find an answer for this question. In my application the form gets sent via AJAX then in PHP it is validated and added to the database. However, I can send a success message but I do not know how to send a friendly error message back to the user.
Below is my JS:
$(document).ready(function(){
var form = $('form');
var submit = $('#submit');
form.on('submit', function(e) {
e.preventDefault();
$.ajax({
url: 'ajax_comment.php',
type: 'POST',
cache: false,
data: form.serialize(),
beforeSend: function(){
submit.val('Posting...').attr('disabled', 'disabled');
},
success: function(data){
var item = $(data).hide().fadeIn(800);
$('.new-comment').append(item);
form.trigger('reset');
submit.val('Submit Comment').removeAttr('disabled');
},
error: function(e){
alert(e);
}
});
});
});
I have little experience in AJAX so please be simple with me! The error messages will be along the lines of 'Username does not exist' etc so I don't think I could do this with client side validation. Thanks in advance!
EDIT: Below is my PHP code that I am using.
<?php
// Include files
include 'config.php';
// Variables
$order_id = $_POST['order_id'];
$comment = $_POST['comment'];
$reviewed = 1;
$date = date('dS F Y');
// Find order ID match
$stmt = $con->prepare("SELECT order_id FROM transactions WHERE order_id = ?");
$stmt->bind_param('i', $order_id);
$stmt->execute();
$stmt->store_result();
$no_id_match = $stmt->num_rows;
$stmt->close();
// Check if review has already been submitted
$stmt = $con->prepare("SELECT order_id FROM transactions WHERE order_id = ? AND review = 1");
$stmt->bind_param('i', $order_id);
$stmt->execute();
$stmt->store_result();
$num_rows_reviewed = $stmt->num_rows;
$stmt->close();
if(empty($order_id) === true || empty($comment) === true) {
exit();
} else if($num_rows_reviewed> 0) {
exit();
} elseif($no_id_match == 0) {
exit();
} elseif(strlen($comment) > 499) {
exit();
} else {
//Insert review into DB
$stmt = $con->prepare("INSERT INTO reviews (order_id, comment, date) VALUES (?, ?, ?)");
$stmt->bind_param('iss', $order_id, $comment, $date);
$stmt->execute();
$stmt->close();
// Update transactions to show review added
$stmt = $con->prepare("UPDATE transactions SET review = ? WHERE order_id = ?");
$stmt->bind_param('ii', $reviewed, $order_id);
$stmt->execute();
$stmt->close();
// Get name from order ID
$stmt = $con->prepare("SELECT first_name, last_name FROM transactions WHERE order_id = ?");
$stmt->bind_param('i', $order_id);
$stmt->execute();
$stmt->bind_result($first_name, $last_name);
$stmt->fetch();
$stmt->close();
$name = $first_name. ' '. mb_substr($last_name, 0, 1);
// Output review live to page ?>
<div class="comment-item">
<div class="comment-post">
<h3><?php echo $name; ?>: <span><?php echo $date; ?></span></h3>
<p><?php echo $comment; ?></p>
</div>
</div><?php
}?>
At the moment the exit() variables are where I would like to validate the script. However since I do not know how to do this right now I am just exiting the script.