gpt4 book ai didi

php - php-ajax 表单提交后不显示 Bootstrap 警报

转载 作者:行者123 更新时间:2023-12-01 01:53:46 24 4
gpt4 key购买 nike

我试图在用户提交表单时显示警报,无论是否成功。该脚本在提交数据时工作正常,但未显示警报。代码是-

重要 - 该表单处于 Bootstrap 模式。

HTML 是:

<div class="col-md-12">
<form id="inquiry-form" method="post" action="inq.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="form-group">
<label for="Name">Name</label>
<input type="text" name="name" class="form-control" id="exampleInputEmail1" placeholder="Name" required="required" data-error="Name is required."/>
<div class="help-block with-errors"></div>
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>

我的 php 脚本是:

try
{
$sql = mysqli_query($conn,"INSERT INTO wig (fname, lname, email,msg)
VALUES ('$fname', '$lname', '$email','$msg')");



$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);

header('Content-Type: application/json');

echo $encoded;
}
else {
echo $responseArray['message'];
}

处理表单提交的jquery是:

$(function () {

$('#inquiry-form').validator();

$('#inquiry-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "inq.php";

$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;

var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable fade in role=alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#inquiry-form').find('.messages').html(alertBox);
$('#inquiry-form')[0].reset();
}
}
});
return false;
}
})
});

最佳答案

尝试将 die 添加到您的 php 脚本中,如下所示:

    echo $encoded;   
}else {
echo $responseArray['message'];
}
die();

关于php - php-ajax 表单提交后不显示 Bootstrap 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40046109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com