Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMUZ140770 Selestino Gama
4,980 PointsHow can you use javascript alert in php
Can anyone help me? i have my code below in php with a form validation. My problem is i tried to use Javascript to echo error message and thank you message but when i use die(); the message pops up but the page is not redirected and when i remove die(); the page is redirected but the message doesn't pop up.
<?php
require 'vendor/autoload.php';
date_default_timezone_set('Africa/Harare');
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
function phpAlert($msg_error) {
echo '<script type="text/javascript">alert("' . $msg_error . '")</script>';
}
//$log = new Logger('name');
//$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
//$log->addWarning('Foo');
$app = new \Slim\Slim( array(
'view' => new \Slim\Views\Twig()
));
$view = $app->view();
$view->parserOptions = array(
'debug' => true
);
$view->parserExtensions = array(
new \Slim\Views\TwigExtension(),
);
$app->get('/', function() use($app){
$app->render('home.twig');
})->name('home');
$app->get('/', function() use($app){
$app->render('about.twig');
})->name('about');
$app->get('/contact', function() use($app){
$app->render('contact.twig');
})->name('contact');
$app->post('/contact', function() use($app){
$name= $app->request->post('name');
$age=$app->request->post('age');
$email=$app->request->post('email');
$expected_date=$app->request->post('expected_date');
$country_to_visit=$app->request->post('country_to_visit');
$reason_for_visit=$app->request->post('reason_for_visit');
$payment_method=$app->request->post('payment_method');
$msg=$app->request->post('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 {
phpAlert( "All the fields are needed. Please fill all fields and send again" );
die();
$app->redirect('/contact');
}
$transport = Swift_MailTransport::newInstance('/usr/sbin/sendmail -bs');
$mailer = \Swift_Mailer::newInstance($transport);
$message= \Swift_Message::newInstance();
$message->setSubject('Email From Our Website');
$message->setFrom(array($cleanEmail=>$cleanName));
$message->setTo(array('selesgama@gmal.com'));
$message->setBody($cleanMsg);
$result = $mailer->send($message);
if($result > 2){
phpAlert( "Thank you for contacting us. We will get to you soon." );
die();
$app->redirect('/');
} else{
phpAlert( "There was an error sending the message. Try again later." );
die();
}
$app->redirect('/contact');
});
$app->run();
?>
MOD: edited to format code.
2 Answers
Christopher Hubbard
392 PointsSelestino,
The PHP function die is a similar construct to exit - meaning that when you call it, the current script will be terminated (i.e. nothing after will be run).
MUZ140770 Selestino Gama
4,980 Pointsi understand, so how can i make my javascript work inside my code