I need to add a tracking code to a Magento template, pretty simple, but I need it on the confirmation page when a user register, but when it does, he's redirected to the index page, as any user who log in.
I think to add a step by override the account controller because there no such configuration (or I wasn't able to find it), but even if it's just a method override of confirmAction to handle the redirect, it doesn't look like the best way, because this redirect is called several time depending on several things and add session message.
Any ideas?
The only real way I know that you could do this would be to hook into the controller_action_postdispatch_customer_account_createPost event. If you don't know how to do that, check out this Wiki page.
In your observer, do something like this:
// Store a simple boolean that flags this user as just having registered
Mage::getSingleton('core/session')->setUserJustRegistered(true);
Then, in any footer template, do something like this:
<?php if (Mage::getSingleton('core/session')->getUserJustRegistered()): ?>
<!-- INSERT TRACKING CODE HERE -->
<?php Mage::getSingleton('core/session')->setUserJustRegistered(null); ?>
<?php endif; ?>
Note that we reset our session variable back to null so that your tracking code doesn't end up firing on every page.
Related
We have a Drupal 7 webform that redirects to a url upon successful submission.
What we need to do is redirect the user if they land on the same webform again and have already submitted.
Do we need a module for this, or do it programmatically?
Thanks in advance.
I looked through the webform module and didn't find any setting that will redirect the user if the user has already submitted a form, so I think you need to do it programmatically.
Note: It might be possible without a custom module by using the rules module. I haven't tried this.
To do it programmatically you could do something like below. It implements the hook_node_view() and checks if the user has already submitted anything by using the webform api function webform_get_submission_count(). (edit: the custom module in this example is called example_webform)
<?php
/**
* Implements hook_node_view().
*/
function example_webform_node_view($node, $view_mode, $langcode) {
global $user;
module_load_include('inc', 'webform', 'includes/webform.submissions');
$submission_count = webform_get_submission_count($node->nid, $user->uid);
if (!empty($submission_count) && $submission_count > 0) {
$redirect = $node->webform['redirect_url'];
drupal_goto($redirect);
}
}
As it is now it will reuse the page that is used when the form is submitted, so if you choose to do this remember to make the success page reflect this. (E.g. it would be strange for the success page to say "your post has been saved" if the user lands on it for the second time.) Or you could replace the $redirect with another page than the one from the webform setting.
Also note that the webform will still add the message "You have already submitted this form. View your previous submissions." if this is enabled.
So here is the solution that we ended up going with.
I saved the webform and made it available as a block
I created a page to hold the webform
I configured the block to appear above the page content
In the page content I put in some javascript to detect if the form element was present - if not forward to the correct url
So the webform redirects correctly upon submission(set in the webform settings), and it then redirects if the user lands back on that page and has completed the webform.
I am trying to send a form via post-method, and with the action
action="<?php echo $_SERVER['PHP_SELF'];?>"
but it is only working if i leave the action blank. This is still reloading the page, just as PHP_SELF. But could this lead to any problems?
Btw, when I use get-method, the action can be PHP_SELF.
The problem is $_SERVER['PHP_SELF'] returns the name of script. You probably mean $_SERVER['REQUEST_URI']. It is working with actoin because most of browsers will submit it to the same page when there is no action attribute.
When I click on a submit button i want the page to redirect to the following page?
header('Location: /pdp/policy-info.phtml');
I wrote the above code in the controller code but I am not able to redirect to the above page. it stays on the same page.
the filename is called policy-info.phtml in the view.
Also once I redirect, would I be able access my form values through $_POST?
Or is there an alternative.
ok it sounds to me like you may be missing a few concepts:
You will never redirect to a phtml file. (unless you have written some custom rewrite/route rules) Zend uses the MVC architecture, urls exist in this fashion: /module/controller/view/key1/value1/keyx/valuex/
generally zend urls don't terminate with file extensions. Also you will never directly call a view file from your browser.
In your form tag, you specify where the form submits to with the action attribute. For your url i'm assuming the pdp controller and policy-info action
action="/pdp/policy-info/"
If you want to redirect after a form submit from with your controller you would use:
$this->_redirect('/pdp/policy-info/');
# maybe you want to execute some code and then execute
# additional code in another controller without re-bootstrapping
$this->_forward('policy-info', 'pdp');
http://framework.zend.com/manual/en/zend.controller.action.html#zend.controller.action.utilmethods
If you redirect you will not have access to your POST unless you saved those values elsewhere (like in your session). If you forward, I believe the values will still be available in the second action.
actually there maybe a few ways to do what you want to do. I haven't tried this first method yet but it should work.
Render a new veiw script from your controller/action if isPost():
public function myAction(){
$form = My_Form();
$this->view->form = $form;
//if form is posted and submit = Submit
if ($this_request->isPost() && $this_request->getPost()->submit == 'Submit') {
if ($form->isValid($this->_request->getPost()) {
//this is where you want to capture form data
$data = $form->getValues();
//render a new viewscript or _forward to a new action and perform your processing there.
$this->render('path to phtml file');
//if user needs to press a button to accept submit = accept
...do some more stuff...
}
}
}
I think this or some variation will work.Note: I don't think _forward resets the request object, so your $_POST data should not be affected.
Also if this policy-info does not require additional input from the user and is just informational you could easily just _forward('action') to a blank action and the router will display the view script.
I would like to make a "preview container" for form values in Yii. (so every time the user finishes entering data, the "preview container" below the form will display them, to let the user knows how the item actually looks like).
To achieve this, the only way is to call a Javascript function to update the "preview container" (using jQuery). The CActiveForm is:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id'=>'item-form',
'enableAjaxValidation'=>true,
));
?>
How do we modify it to call a javascript function each time the fields are validated?
(Note: whenever we switch between the input fields, the fields are validated dues to enableAjaxValidation=>true)
Thanks in advanced.
With jQuery you can define your own listener functions for the fields you want to update, which is probably going to be cleaner than trying to hook into the validation functions.You could monitor onchange or blur or whatever is most appropriate to your data.
The js can be loaded via Yii's registerScript function or, again, whatever is most appropriate for your app. A listener function would normally be loaded on DOM ready, i.e., with the POS_READY attribute for registerScript.
You can search the tutorials as well as this basic tutorial for more info.
I have a form (login form) that appears in all pages (if the user is not logged in).
The form is created from view helper and then directly called in layout.phtml
class Zend_View_Helper_LoginForm extends Zend_View_Helper_Abstract
{
public function loginForm()
{
$login = new Application_Form_Login();
return $login;
}
}
<?php if(!$this->isLoggedIn()): ?>
<div id="login">
New User? Register Here
<?php echo $this->loginForm(); ?>
Forgot Password!
</div>
<?php endif; ?>
Now how to set the action of this form such that i could validate the fields in the same page?
Matthew Weier O'Phinney (the ZF Programmer leader ) has a blogpost about creating your reusable widget exactly like your needs
you should try it ,
http://weierophinney.net/matthew/archives/246-Using-Action-Helpers-To-Implement-Re-Usable-Widgets.html
You can validate it on other page and redirect back after validation. Also, you can validate it through ajax.
What we usually do is that we send the form to different action and have dedicated form rendered in that actions view. If there is no error, user is redirected without noticing. If there is an error, the form is dispalyed in main content area with proper error messages. Usualy the problem is that the area of this form vidget is not big enough for good error message.
usually you post it to login page and if you fail you can redirect user back, set proper http header and display a error message