I have a page powered with PHP and AJAX, when a user submits one of my forms I check for errors in the getData.php script.
This example is if the user submits the form with the default value I'm wondering if there is a way to pass back those errors or trigger the AJAX to fire an error if the user commits on or if I need to do error handling before the AJAX call
$('form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: '_ajax/addData.php',
data: $('form').serialize(),
success: function () {
$("input").val('Info Here');
$("form").hide();
reloadInfo();
}
});
});
PHP
$info = $_POST['info'];
if($info != 'Info Here') {
$conn = mysqli_connect();
$query = "INSERT INTO leads VALUES(0, '$companyName', 1, NOW(), 3)";
$result = mysqli_query($conn, $query) or die ('Error Could Not Query');
$id = mysqli_insert_id($result);
header("Location: http://localhost/manage/info.php?id=$id");
mysqli_close($conn);
} else {
echo '<script>alert("Error");/script>'
}
PART 2