PHPMailer - Show success message after redirect - laravel-4

I building a very simple app with Laravel4 and so far i have managed to set up PHPMailer to work with a contact form, the users fill in their details and send me an email, normal stuff, everything works fine.
After the user sent the email successfully, he is redirected to the home page via
if($m->send()) {
header('Location: /path/to/home/');
die();
}
Now what i need is a success message that appears at the top of the homepage if the user has been redirected after a successfully sent email.
I have a div with .success class sitting on top of my home page, absolutely positioned out of view, with a negative Y value.
I tried pulling it down after on $m->send() like so:
if($m->send()) {
header('Location: /path/to/home/');
echo "<script type='text/javascript'>
$('.success').animate({
top: 0
}, 2000);
</script>";
die();
}
but it didnt work. In fact, nothing i echo after the header() has any effect.
What can I do?
Thank you guys!

This is simple HTTP - when you set the Location header, it's telling the browser to leave the page you're on and go somewhere else - anything that happens afterwards (like that little JS snippet) will never reach the browser. You need to put that snippet on the page you're redirecting to, not on this one.

I solved the problem after realizing that you can't use jquery on the document before you actually link the jquery lib in.
So, in my phpmailer config file, I set $_SESSION['success'] = true before i redirect with the header('Location: /path/to/home/'); , and then, on the Homepage, the page I wanted the success message to be displayed on, I added this bit of code (AFTER linking the jQuery library):
<?php
if(isset($_SESSION['success']) && $_SESSION['success'] == true ) {
?>
<script type='text/javascript'> $('.success').animate({top : 0}, 'normal').delay(3000).animate({top : -57}, 'normal');</script>
<?php
} else {
$_SESSION['success'] = false;
}
?>
I don't know if this is a good practice but it does work.
I also had to session_start(); on my Homepage (obviously).
Hope this helps anyone in the same situation!

Related

client side validation not working for model window / ajax-loaded-form in yii

I am using Yii-user extension in the main layout i have a sign up link which is common to all the Cmenu
view/main layout
echo CHtml::link('Signup','#',array('id'=>'regi'));
$("#regi").click(function(){
$.ajax({
type:'GET',
url:'<?php echo Yii::app()->request->baseUrl;?>/index.php/user/registration',
success:function(res){
$("#dispdata").show();
$("#dispdata").html(res);
}
});
});
<div id="dispdata"><div>
**yii user extension **renders this perfectly and even submit its correctly if form values a re valid.
but if the values are incorrect and blank it redirect to url .../user/registration
which is not what my need .I need guidance what do i do such that if the values are incorrect or blank it should not redirect and display the errors in model window.
I did tried but hardly could get the satisfied results
if i place the following the model window itself doesnt appear what do i do
module registrationController i placed
....//some code here (**in yiiuser register controller**)
if ($model->save()) {
echo CJSON::encode(array(
'status'=>'success',
));
}
....//some code here...
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
$this->renderPartial('registration',array('model'=>$model,),false,true);
in module view registration
<?php echo CHtml::ajaxSubmitButton(Yii::t('registration'),CHtml::normalizeUrl(array('user/registration','render'=>false)),array('dataType'=>'json',
'success'=>'function(data) {
if(data != null && data.status == "success") {
$("#registration-form").append(data.data);
}
}')); ?>
can anyone please guide me am working past 10 ten days tried every hook or crook method but could not obtain the results......how can the model window with client side validation be done appear..... Please guide me or let me know something better can be done
rules in registration model
if (!(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')) {
array_push($rules,array('verifyCode', 'captcha', 'allowEmpty'=>!UserModule::doCaptcha('registration')));
as well was not with attributes for reqired field
have changed to
array_push($rules,array('verifyCode', 'captcha','message' => UserModule::t("captcha cannot be blank.")));
and added the verifycode to required field
yet not working,
The simple way is using render method in your Ajax action and creating empty layout for this action. If you do so, validation scripts will be included in the server response. Also you need to exclude jquery.js and other script with Yii::app()->clientScript->scriptMap and include them in main layout always.

ajaxbutton how to prevent refresh of the page

I succeed to use Ajax with Yii framework.
I renderPartial a form from within a list of post.
What I want to do is to prevent refresh when the user click on the ajaxbutton in the form.
In the beginning of the form I used the following code to activate ajax
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'post-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
)); ?>
and at the end of the page I simply have the ajaxbutton
<?php echo CHtml::ajaxSubmitButton('Save'); ?>
in the action controller I have the following
if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
echo CActiveForm::validate($comment);
Yii::app()->end();
}
When I click on the ajax button, it saves the data but refresh the page, so it display the form.
What I want is to stay on the page.
Is anyone to help ?
Thank you in advance.
you can prevent the page from refresh, or ask the user if he's sure he want to leave the page by this code:
window.onbeforeunload = function() {
return "Dude, are you sure you want to leave? Think of the kittens!";
}
you can check this question: Prevent any form of page refresh using jQuery/Javascript
I think you have some thing like $this->redirect( ... ));
in your controller after $model->save() , right?
so don't redirect there

how can I redirect in .jsp while working with Ajax

I am developing application using Ajax and jsp.
My index.jsp page has HTML code for Login and some Ajax code in javascript. Ajax will call my another CheckLogin.jsp page
CheckLogin.jsp page checks on server for valid username and password. It returns "success" if it's valid otherwise will return message stating "username or password is not valid."
Now, when username and passwrod is valid, instead of success, I want to redirect the page to "Home.jsp" what should I do?
I am new to jsp. I appreciate your help on this.
JSP code gets run once on the server before it goes to the client, so JSP/JSTL cannot do a redirect following the success or otherwise of an AJAX call (without a full page refresh - which obviates the use of AJAX). You should to do the redirect with Javascript:
if (success) {
var successUrl = "Home.jsp"; // might be a good idea to return this URL in the successful AJAX call
window.location.href = successUrl;
}
On successful AJAX call/validation, the browser window will reload with the new URL (effectively a redirect).
Since I don't see your code, you can integrate this somewhere inside your validation :
<%
pageContext.forward("logged.jsp");
%>
function Edit() {
var allVals = $('#NEWFORMCampaignID').val();
if (allVals > 0) {
window.location = '/SMS/PrepareSMS?id='+allVals;
}
else
alert("Invalid campaign to Edit")
}
In order to redirect using Button click and pass some parameters, you can call the Controller Path(#RequestMapping(value="/SMS/PrepareSMS")) and then handle it there..

Magento redirect after logout

how can I redirect the customers after logout to default store view in magento?
In logout I redirect them another store view.
I know it's not elegant, but the easiest method I have found is to copy and modify the template file at app/design/frontend/base/default/template/customer/logout.phtml to your own theme directory.
Specifically this line:
<p><?php echo Mage::helper('customer')->__('You have logged out and will be redirected to our homepage in 5 seconds.') ?></p>
<script type="text/javascript">
//<![CDATA[
setTimeout(function(){ location.href = '<?php echo $this->getUrl() ?>'},5000);
//]]>
</script>
By modifying location.href url and even the timeout you can point the user to anywhere just after logout. E.g.:
<script type="text/javascript">
//<![CDATA[
setTimeout(function(){ location.href = '<?php echo $this->getUrl('*/*/login') ?>'},500);
//]]>
</script>
Again, it's not elegant, but it should be a quick enough redirect that the quick hop on the page will then shove them to another url, in the above example, back to the login screen.
Unfortunately there's not a convenient event hook to manipulate the logout redirect location.
Mage_Customer_AccountController::logoutAction() sets a redirect to ::logoutSuccessAction() on the response object after the customer_logout event is dispatched, and it's the rendering of the customer/logout.phtml template which uses PHP to set echo a javascript param to redirect to the homepage with no OOB possibility to pass an arg for an alternate JS-based redirect.
I think the cleanest solution would be to observe controller_action_postdispatch_customer_account_logout, grab the controller object, and overwrite the location header using the response object's setRedirectWithCookieCheck() method:
public function logoutRedirect($obs)
{
$obs->getControllerAction()
->setRedirectWithCookieCheck(/* your URL param(s) */);
}
Write the following method in Your Model > Observer .
public function customerLoggedOut(Varien_Event_Observer $observer)
{
$observer->getControllerAction()
->setRedirectWithCookieCheck(CustomUrl);
}
Customurl is a url on which you want to redirect after Logged Out.
If you want complete solution for custom url redirection for your ecommerce website after logged In, Logged Out and Registration. Custom Redirection extension can help you. Click on link to get extension. http://www.magentocommerce.com/magento-connect/custom-redirection.html

Using browser back button in triggers flash message in CakePHP again

I just came across a bit of a puzzling situation. My controller action looks like this:
public function myaction($eventId = false) {
if(!$eventId) {
//list all events
$data = foo;
} else {
if(!$this->Event->findById($eventId) )
{
$this->Session->setFlash('myerror message', 'flash_frontend_message');
$this->redirect(array('controller' => 'events', 'action' => 'myaction'));
} else {
// display event information
$data = foo;
}
}
$this->set($data);
}
If I call the function with an $eventId that is not found in the database, it outputs an error message and the user gets redirected back to the list of all events. However lets say I then select an eventId which is valid, view the relevant information and then press the browser back button, the previous flash message gets out put again, even though the URL in this case does not contain the $eventId.
I suppose what happens is that the page gets loaded from the browser cache rather then reloaded. I have tried to avoid storing the view in the cache like this:
<!--nocache-->
<?php echo $this->Session->flash(); ?>
<?php echo $this->Session->flash('auth'); ?>
<!--/nocache-->
but still, the flash message gets displayed.
Any idea how to prevent this behavior?
Is there perhaps a way to clear the flash after it has been displayed?
you are talking about the right cache but trying to fix it with the wrong cache.
<!--nocache-->
is php/html no cache statements in the file itself. it has nothing to do with the browser cache.
what you need to be doing is setting the right headers.
there is a convinience method in the request object for this exact thing:
http://book.cakephp.org/2.0/en/controllers/request-response.html#interacting-with-browser-caching
I use it in all my normal frontend actions via AppController:
public function beforeRender() {
$this->response->disableCache();
...
}

Resources