I want to execute a Action and stay on the same page.
I created the link to eh action like this:
<?php echo(link_to('Add to Watchlist', 'housing/addToWatchlist')) ?>
which executes this action with a redirect
echo('ADDING TO THA WATCHLIST');
$referrer = $request->getReferer();
return $this->redirect($referrer);
as suggested here: symfony link to change language and stay on the page
This solution works but unnecessary reloads the page, which may be necessary to change the language but not to add an item to a Watchlist.
Without reloading the page you need to use Javascript and an Ajax approach.
http://www.symfony-project.org/jobeet/1_4/Propel/en/18
Also check out link_to_remote.
Related
I have a module which had instaled a CMS page with some default URL and some text with form. On this form I have fields and submit. I validate my fields using ajax(send POST in controller of my module). If validation is OK, I redirect to the same page with "succes message".
The problem is, that the default URL of this CMS page in which I make redirect after succes submit can be changed in BO - that's why I cannot just $this->_redirect('default_URL') in my controller, because this url can be changed.
What should I do?
EDIT : Solution: use $this->_redirectReferer() in my controller after success validation
Try to put "back URL" to your form as a hidden field or you can use $this->_redirectReferer() to redirect back to CMS page.
See http://docs.magentocommerce.com/Mage_Core/Mage_Core_Controller_Varien_Action.html#method_redirectReferer
If you want to redirect to some certain CMS page (not the one where form is placed) you can do this:
Add dropdown to system configuration to be able to select "Success page" (much better than hardcode cms page ID)
Redirect to this page in your controller
Code:
$pageId = Mage::getStoreConfig('mymodule/config/success_page');
$page = Mage::getModel('cms/page')->load($pageId);
$this->_redirect($page->getUrlKey());
You can get a store config by using the getStoreConfig method, something like
Mage::getStoreConfig('helloworld_options/messages/hello_message');
See Alan's blog, about detailed instructions how to create custom config values (which you might already have) and to access them.
Use this code for redirect to referer
<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?>
For example in custom login form:
<form method="post" action="<?php echo Mage::helper('customer')->getLoginPostUrl() ?>">
<?php Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri());?>
...
...
I have an index.php with two major sections: the navbar and the main-content. The navbar contains links which will load another webpage to the main-content through this jQuery code:
jQuery('#main-content').load('sampleurl');
Some of these web pages contain links to another web page, so I want to add a back button.
I tried using the history.back() and history.go(-1), as well as the $_SERVER['HTTP_REFERER'], but they don't really work in my case.
How will I add the back button in this situation?
You should keep your last viewed page in JavaScript variable or in value of hidden input and then you only need to add button with
jQuery('#main-content').load(old_url);
You must always update your variable when you load your next page via jQuery('#main-content').load('sampleurl');
Try manipulating the javascript location or location.hash
https://developer.mozilla.org/en-US/docs/DOM/window.location
Of course, you need to be able to turn your URL back into the relevant page content too.
In conjunction with both Charlie and Michael's answers, since you are already using jQuery, another option is to include the jQuery Address plugin.
You can implement this idea, by creating a function to read a hash path to load new content. Following this, the content can be navigated by the built-in back + forward buttons.
The only challenge I foresee with this implementation is associating the new content with the hash path.
Good luck!
Just an idea: Add hash to Url whenever you load the page; and then you can use history.back()
$(function(){
if(window.location.hash === 'sampleurl'){
jQuery('#main-content').load('sampleurl', function(){
window.location.hash = 'sampleurl'; //<<-- match with your loaded page;
});
}
});
good luck !
I am currently working on the project that have 2 php files category.php and product.php.
In category.php file i put a link shown below.
click here
I have the following js function called on the body onload in the product.php page.
<body onload="font_select(1760,'No',239)">
When users click on the link then they will be redirect to product.php file where the body onload function is called.this function is working fine.
My question is i need to call this function only once when the user enter in product.php page.currently this function is called on page reload also.
How would i call this function only once ? Thanx In Advance.
The most efficient way will be, use Cookies
(It's a little kludgy, but you could check document.referrer in font_select)
well that's wrong, turns out at least in firefox the referrer does not change apparently when the page is refreshed.
You may want to have a look at this question, where they handle detecting refresh using a cookie, however you should know that is really just a check if the page has been accessed before so it won't work unless you also clear that cookie on detecting a click on the link from your category.php page.
Go for javascript cookies
well to track whether the function is called or not, you could use javascript cookie. Like say you set a cookie with any name, say functioncalled with value 0 initially, when user goes to product page, check this cookie. If value is 0, then call a function then set the value of cookie to 1, next time when page is reloaded the function will not be called.
In my PHP site, when a user adds something to their cart I trigger a URL change to make some functions and triggers on the page:
Original URL
/category/product/10
New URL
/category/product/10#addtocartbutton
URL becomes
/category/product/10?x=1
The method:
/category/product/10
/category/product/10#addtocartbutton (user clicks an onclick href to submit form)
/category/product/10?x=1 (the form adds the item to the session and appends this URL querystring)
The problem is when they press the bac b
In Javascript, using location.replace instead of location.href = 'new location'
Take a look at: http://www.roseindia.net/javascript/javascript-location-replace.shtml
-> location.replace will replace the current address by the new one -> when user click back button -> error will not happen.
Or try something with pushState. You should be able to manipulate you're history according to your needs.
A good idea is to make the add function redirect you back to the product page without showing the query-string. Since I dont know how you wrote your add controller it's hard to give you an exact code example...
Write the PHP controller for adding something like:
function add_to_cart()
{
add $_GET['x'] to session;
redirect back to product page;
}
In the settings.php i have some input for the realname,hobby,city and select tag for the languages
The html form is easy and i'm not going to copy it :)
This is the php code for the form
<?php
if(isset($_POST['submit'])){
if($this->edit->process()){
$s = 1;
}
}
if($s){echo '<p id="success">Success</p>';}
?>
This is in the view file and when the form is submit the $this->edit = the model for the updating the user data.
Everything work really good but in head.php there is this code
<?php $lang = $this->lange('global',$this->getUser->language($_SESSION['userID']));?>
With him this get the user current language and after that load the file with lange()
So if the submit is like this,the inputs and select tag are changing with what the user have choicen but the to see the new language they have to refresh the page. This is not good,because the user may be confuced that they did something wrong.
But if the form with method="POST" the page is refreshing and still with the old language
It is going to be quite hard to change all the texts in the DOM model to make the language change without redirect. I doubt it's really your desire.
You ought to make a GET method redirect after processing any POST form. So, instead of printing whatever "success" messages you have to reload the page.
Using session to store the language is not good method, the language have to be set by means of the page address - a subdomain (preferable) or a virtual directory.