【发布时间】:2018-12-13 19:28:56
【问题描述】:
我需要在我的 php 中调用一个 js 函数,但它不起作用。有人可以告诉我我做错了什么吗?我怎样才能以简单的方式做到这一点?
谢谢!
我有三个文件:
邮件.php
负责发送我的 $_POST 东西(它工作正常)。我调用我的 javascript 函数来切换模式,具体取决于邮件是否已发送。
<?
...
$response = $sendgrid->send($email);
if ($response) {
echo '<script type="text/javascript">',
'confirmaEnvio();',
'</script>';
header('Location: /contato');
}
else {
echo '<script type="text/javascript">',
'falhaEnvio();',
'</script>';
header('Location: /contato');
}
?>
functions.js
两个功能,一个是用一个告诉邮件已发送的文本来切换模式,一个是用一个告诉邮件没有发送的文本来切换模式。
function confirmaEnvio () {
$("#modal").find(".modal-body").text("Mensagem enviada com sucesso!");
$("#modal").modal("show");
}
function falhaEnvio() {
$("#modal").find(".modal-body").text("Erro!");
$("#modal").modal("show");
}
view.html
表单使用 POST 向 mail.php 发送数据
<form name="contato" method="post" action="../php/mail.php">
<div class="form-group row">
<label for="email" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required>
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Nome</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="Nome" required>
</div>
</div>
<div class="form-group row">
<label for="msg" class="col-sm-2 col-form-label">Mensagem</label>
<div class="col-sm-10">
<textarea class="form-control" name ="msg" rows="10" id="msg" placeholder="Mensagem" required></textarea>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" name="submit" class="btn">Enviar</button>
</div>
</div>
我的模态,同一个文件,view.html
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
【问题讨论】:
-
在设置标题之前不能回显任何内容。应该从中得到服务器端错误
-
确保在调用 php.ini 之前声明了 confirmaEnvio 和 falhaEnvio。如果它位于页面的末尾,请将其移至顶部。与模态html代码相同。
-
通常情况下,该过程与您似乎正在尝试的不同。它通常涉及:浏览器对 Web 服务进行 AJAX 调用,Web 服务可能以 JSON 格式返回结果,浏览器根据 Web 服务返回的结果显示模态或其他。
-
我也试过不带标题,它不起作用,模式永远不会显示。 @charlietfl
标签: javascript php html forms post