【发布时间】:2021-05-08 12:06:03
【问题描述】:
我觉得我错过了一些非常简单明了的东西:
我正在使用 GET 从我的 php 文件将响应发送回前面:
添加.php:
foreach ($quote as $key => $value) {
if (empty($value) or !isset($value)) {
$error = "$key field is empty";
}
}
if (!empty($error)) {
// send error message to front
$url = "index.php?error=". urlencode($error);
header("Location:$url");
exit;
}
我实际上可以在调试器中看到 index.php 获取了这个变量并且它通过了代码,但是我的 div 仍然没有显示在页面上:
索引.php:
<body>
<div class="container">
<h1>some text here</h1>
<div class="alert alert-success success my-3" style="display:none">Done!</div>
<?php
if(isset($_GET['error'])) {
echo(<<<EOT
<div class="alert alert-danger fail my-3">{$_GET['error']}</div>
EOT);
}
?>
使用 AJAX 处理成功消息:
<script>
$(document).ready(function() {
/**
* Send data after click on submit button
*/
$(function() {
$("#add").on('submit', function(e) {
// stop the following function from happening before click on submit button
e.preventDefault();
// send ajax request
$.ajax({
url: 'add.php',
data: $("#add").serialize(),
type: "post",
success: function(response) {
// check server response
if (response == "success") {
// show success
$('.success').fadeIn(500);
setTimeout(function () { location.reload() }, 1000);
}
//else {
// show error message on failure
// $('.fail').fadeIn(1000);
//}
}
});
});
});
})
</script>
任何帮助将不胜感激!
【问题讨论】:
-
您使用了多少个文件?前后端文件名?
-
index.php 用于前面,add.php 用于后面
-
显示这些文件的完整代码。如果代码太多,则精确显示。
-
我已经用更多细节编辑了这个问题,谢谢!
标签: php html error-handling