所以我正在制作联系表。提交表单时,我想向 Bootstrap 模式添加成功或错误消息。
这是提交表单并打开模式的按钮。
<input class="btn btn-info" type="submit" value="Submit" name="Submit" data-toggle="modal" href="#contactModal"/>
因此,当单击按钮时,它会提交表单并打开模式。有没有办法在 success
上动态地将文本字符串添加到模式中或failure
脚本?
这是当前 PHP 成功/失败操作
if ($success){
print "<meta http-equiv="refresh" content="0;URL=contactthanks.html">";
}
else{
print "<meta http-equiv="refresh" content="0;URL=error.html">";
}
但我希望它将内容添加到模式中,而不是添加 <meta>
重定向页面的标签
这里是模态代码
<div id="contactModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Thanks!</h3>
</div>
<div class="modal-body">
<p>I'm pretty busy sometimes but I'll try my best to message you back <strong>ASAP</strong>!</p>
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
那么我会怎样 print
到 <div>
的内部在模式中无需刷新页面或重定向?
我不懂 PHP 或 AJAX,而且我还没有开始学习 JS 和 jQuery(准确地说是两天)
最佳答案
您无法在不刷新页面的情况下提交表单。您最可能想要做的是对 php 使用 AJAX 请求,该请求将以 JSON 形式返回成功/失败状态,然后您将使用它通过 javascript 有条件地将内容添加到您的 div 中。
您将只使用普通的 <input type="button" />
而不是表单。与 onclick="someFunction"
其中 someFunction 进行 AJAX 调用。
您将需要使用 jQuery。
你的 php 看起来像
//do something with submitted values
if ($success){
echo json_encode(True);
}
else{
echo json_encode(False);
}
AJAX 调用类似于
$.ajax({
type: "POST",
url: "dosomething.php",
data: { formValue1 = <get value using jquery>, etc... }
dataType: "json",
success: processData,
error: function(){ alert("failed"); }
});
function processData(data)
{
if(data) {
$("div#divid").html("what you want to add inside the div");
}
}
关于php - 用php动态添加内容到div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16555452/