php - 用php动态添加内容到div

标签 php javascript html twitter-bootstrap

所以我正在制作联系表。提交表单时,我想向 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/

相关文章:

php - 使用 Codeigniter 事务插入到两个表

javascript - 如果鼠标不移动,输入中的 OnChange 不会触发

javascript - 使用跨域Ajax进行搜索?

PHP 通过 id 从 db 回显

php - 上传文件时 undefined index

php - 如何提高网站在搜索引擎上的浏览量

javascript - 将第 3 方 JS 库导入 Angular 12 库

javascript - 为什么需要在 D3.js 中点击两次才能触发我的点击事件?

javascript - 把 &lt;script&gt; 标签放在 </body> 标签后面有错吗?

javascript - 降低JavaScript中HTML音频的音量