使用AJAX返回PHP错误处理
我有一个支持PHP和AJAX的页面,当用户提交我的表单时,我会检查getData.PHP脚本中的错误 这个例子是,如果用户使用默认值提交表单,我想知道如果用户提交,或者如果我需要在AJAX调用之前进行错误处理,是否有方法传回这些错误或触发AJAX触发错误使用AJAX返回PHP错误处理,php,jquery,ajax,Php,Jquery,Ajax,我有一个支持PHP和AJAX的页面,当用户提交我的表单时,我会检查getData.PHP脚本中的错误 这个例子是,如果用户使用默认值提交表单,我想知道如果用户提交,或者如果我需要在AJAX调用之前进行错误处理,是否有方法传回这些错误或触发AJAX触发错误 $('form').on('submit', function (e) { e.preventDefault(); $.ajax({ type: 'post', url: '_ajax/addDa
$('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>'
}
$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);
echo $id;
//header("Location: http://localhost/manage/info.php?id=$id");
mysqli_close($conn);
} else {
echo 'something went wrong!';
}
$info=$\u POST['info'];
如果($info!='info Here'){
$conn=mysqli_connect();
$query=“插入leads值(0,$companyName',1,NOW(),3)”;
$result=mysqli_query($conn,$query)或die('Error cannot query');
$id=mysqli\u insert\u id($result);
标题(“位置:http://localhost/manage/info.php?id=$id”);
mysqli_close($conn);
}否则{
回显“警报(“错误”);/script>”
}
第2部分Javascript:
success: function (data) {
if(!data.success) alert(data.errors); // Just for demonstration purposes
$("input").val(data.errors);
$("form").hide();
reloadInfo();
}
PHP:
您的代码有几个问题:
未在任何位置定义$companyName
- 您应该使用准备好的语句,而不是将数据放入SQL查询中
- 您应该将整个AJAX PHP代码放在一个块中
- 在AJAX PHP代码的末尾,编写一些JSON代码
- 我不明白为什么您要尝试重定向AJAX调用,通常您会告诉客户机进行重定向
try {
if(!isset($_POST['info']))
throw new Exception('Post info was not set');
$info = $_POST['info'];
if($info == 'Info Here')
throw new Exception('Invalid value for info');
$conn = mysqli_connect();
if(!$conn)
throw new Exception('Database connection failure');
$companyName = '?????';
$query = 'INSERT INTO leads VALUES(0, ?, 1, NOW(), 3)';
$stmt = mysqli_prepare($query);
if(!$stmt)
throw new Exception('Could not query database');
mysqli_stmt_bind_param($stmt, 's', $companyName);
mysqli_stmt_close($stmt);
$id = mysqli_stmt_insert_id($stmt);
mysqli_close($conn);
echo json_encode(array(
'success' => true,
'new_id' => $id,
));
}catch(Exception $ex){
echo json_encode(array(
'success' => false,
'reason' => $ex->getMessage(),
));
}
替代方法
HTML:将此段落标记放在要显示错误的位置
<p id="display_error"></p>
这是代码逻辑中的一个问题。在PHP中,如果失败,则需要回显错误。客户端一切正常。
success: function(response)
{
if((response !== "") && ($.isNumeric(response))) {
{
//redirect in ajax success
location.href = "http://localhost/manage/info.php?id="+ response;
}
else {
//this will display the custom error.
$("#display_error").html("<p>" + response + "</p>"); //output: something went wrong!
}
}
$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);
echo $id;
//header("Location: http://localhost/manage/info.php?id=$id");
mysqli_close($conn);
} else {
echo 'something went wrong!';
}