gpt4 book ai didi

javascript - 重定向到 php 页面后使用 javascript 显示消息

转载 作者:行者123 更新时间:2023-11-28 06:26:03 25 4
gpt4 key购买 nike

我正在处理联系页面。它使用Slim 3框架来重定向。一切都很好,但我想添加一些 jQuery 代码来通知用户电子邮件是否已发送。这是我的 php 代码:

<?php

require 'vendor/autoload.php';

$app = new Slim\App();

//... slim views codes

$app->get('/', function ($req, $res, $args) {
return $this->view->render($res, "about.twig");
})->setName('home');


$app->get('/contact', function ($req, $res, $args) {
return $this->view->render($res, "contact.twig");
})->setName('contact');


$app->post('/contact', function ($request, $response, $args){
$body = $this->request->getParsedBody();

$name = $body['name'];
$email = $body['email'];
$msg = $body['msg'];

if(!empty($name) && !empty($email) && !empty($msg) ){
$cleanName = filter_var($name,FILTER_SANITIZE_STRING);
$cleanEmail = filter_var($email,FILTER_SANITIZE_EMAIL);
$cleanMsg = filter_var($msg,FILTER_SANITIZE_STRING);

}else {
$path = $this->get('router')->pathFor('contact');
return $response->withRedirect($path);
// javascript: please fill the fields.
}

//sending mail

]);

$result=$mailer->send($message);

if ($result > 0) {
// javascript: your email has been sent
$path = $this->get('router')->pathFor('home');
return $response->withRedirect($path);

} else {
// javascript: error sending mail
$path = $this->get('router')->pathFor('contact');
return $response->withRedirect($path);
}

});

$app->run();

正如您所看到的,基本上有两个页面:“联系人”和“主页”。联系页面中有一个表单,如果表单提交成功并且发送了电子邮件,页面将重定向到“主页”,但如果没有成功,则会再次重定向到“联系”。现在我想添加 jQuery 代码,以便我可以告诉用户电子邮件是否已发送。我的 HTML 中有这样的内容:

<div id="feedback" class="success">
<h3>Success!</h3>
<p>You're email has been sent.</p>
</div>

在我的js文件中:

$(document).ready(function() {

$(".success").click(function() {
$("#feedback").addClass("dismissed");
});

});

非常感谢!

最佳答案

$message = "Your text";
echo "<script type='text/javascript'>alert('$message');</script>";

关于javascript - 重定向到 php 页面后使用 javascript 显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35125190/

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