重定向到php页面后使用javascript显示消息
我正在做一个联系页面。它使用Slim3框架重定向。一切都很好,但我想添加一些jQuery代码来通知用户是否发送了电子邮件。 这是我的php代码:重定向到php页面后使用javascript显示消息,javascript,php,jquery,redirect,slim,Javascript,Php,Jquery,Redirect,Slim,我正在做一个联系页面。它使用Slim3框架重定向。一切都很好,但我想添加一些jQuery代码来通知用户是否发送了电子邮件。 这是我的php代码: <?php require 'vendor/autoload.php'; $app = new Slim\App(); //... slim views codes $app->get('/', function ($req, $res, $args) { return $this->view->render($
<?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();
非常感谢 $message=“您的文本”;
$message = "Your text";
echo "<script type='text/javascript'>alert('$message');</script>";
回显“警报('$message');”;
$message=“您的文本”;
回显“警报('$message');”;
如果表单成功,您可以通过$\u GET发送一些内容,并且您可以在成功后重定向到的页面中创建一条if语句,将javascript消息放在该页面中。为什么不使用$.ajax()
并在联系人页面中检查它是否已发送。如果它是在几秒钟后发送的,则显示消息并重定向,否则显示错误消息而不重定向。如果表单成功,则可以通过$\u GET发送内容,并且可以在成功重定向后放置javascript消息的页面中生成if语句。为什么不使用$.ajax()
并在联系人页面中检查是否发送了该邮件。如果发送,则在几秒钟后显示消息并重定向,否则显示错误消息而不重定向
$(document).ready(function() {
$(".success").click(function() {
$("#feedback").addClass("dismissed");
});
});
$message = "Your text";
echo "<script type='text/javascript'>alert('$message');</script>";