I'm working on a contact page. It uses Slim 3 framework to redirect. Everything is fine, but I want to add some jQuery code to inform the user whether the email is sent or not. this is my php code:
<?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();
As you can see there's basically two page: "contact" and "home". there's a form in contact page, if form submission is successful and a email is sent, page would be redirected to "home", but if not it would redirect to "contact" again. Now I want to add jQuery code so I can tell the user email has been sent or not. I've got in my HTML something like this:
<div id="feedback" class="success">
<h3>Success!</h3>
<p>You're email has been sent.</p>
</div>
and in my js file:
$(document).ready(function() {
$(".success").click(function() {
$("#feedback").addClass("dismissed");
});
});
Thanks a lot!
$message = "Your text";
echo "<script type='text/javascript'>alert('$message');</script>";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.