我正在处理联系页面。它使用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();
正如您所看到的,基本上有两页:“联系”和“主页”。 在联系页面中有一个表单,如果表单提交成功并且发送了电子邮件,页面将被重定向到“home”,但如果没有,它将再次重定向到“contact”。现在我想添加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");
});
});
非常感谢!
答案 0 :(得分:0)
$message = "Your text";
echo "<script type='text/javascript'>alert('$message');</script>";