Altering the back behaviour in the browser PHP - php

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;
}

Related

calling javascript function once on body onload event

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.

how can i redirect a user to custom url after adding page through add.php (Facebook)

How can I redirect a user to just added page after adding it through add.php?
let's say my url looks like: http://www.facebook.com/add.php?api_key=API_KEY&pages=1
After selecting a page from the select box and clicking 'Add MY_APP_NAME' user is redirected to the wall of the page instead of to freshly added MY_APP.
Notice
I know this type of questions lots of here but i have no found any solution that why i put this question here again.... helps are definitely appreciated
Tried
I tried myself like this using Javascript but seriously not success
<script>
if (window == top) {
top.location.href = 'https://apps.facebook.com/abc/' + document.location.href.replace(/https?:\/\/[^/]*\/?/, '');
}
function addToPage(page_id){
top.location.href = 'http://facebook.com/add.php?api_key=<?php echo C_APP_ID; ?>&pages&perms=publish_stream&page='+page_id;
}
</script>
See Image
https://www.facebook.com/dialog/pagetab?
app_id=APP_ID&
display=popup&
next=https://facebook.com
Using this alternative method you can specify the URL to be redirected to after interacting with the dialog to add an application to your page in the next parameter.
You could try adding that parameter to your request.
Other possible alternatives for the next parameter are -
redirect_url
redirect_uri
Both are used elsewhere when dealing with redirecting users - perhaps one of them will assist you as well...

Symfony execute Action and stay on page

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.

Executing PHP: detecting when a link has been clicked

I've got a bit of a dilemma with some PHP code. The site I'm working on has a "Back to Previous Page" option and I'd like it to behave much like a browser's back button. As it stands right now, I'm using a $_SESSION variable to track what the current and previous pages are. I've also "refresh-proofed" the variables so that I don't end up with both the previous and current pages being the same.
So here's the issue:
With the current implementation, if I go to one page, say "register.php" and then go to "forgot.php", the previous page will be "register.php" which is fine. However, if I click "Back to Previous Page" I'll end up back at "register.php" with the previous page being "forgot.php" which now leaves me with a 2-page loop with going back.
I tried implementing SplQueue to help me keep track of variables and I tried using the dequeue() function in my links to get the last page to show up as the link. The problem comes in when the dequeue is actually called and causes the element to disappear so that if I refresh, the element is no longer in the queue and the link changes. I fixed this by "refresh-proofing" the function that calls the dequeue for me and it works as I would like it to. The problem is now forward-linking. If I direct myself to another page, I don't want the old links to dequeue information.
Ex:
I'm on register.php and my previous page is "forgot.php". The "Back to Previous Page" link accurately shows that "forgot.php" is the page it will direct to, but now it's no longer in the queue, so if I go to another page, say "profile.php" and then use the back button to go back to "register.php", it will no longer show "forgot.php" as the page that you will go to if you hit "Back to Previous Page" again.
So, I guess my question is really how I can make a link call a PHP function without actually calling that function UNTIL the link has been clicked. I've tried having the link point to a JavaScript function, but the JS functions tend to tell me that my queue is empty, which is completely wrong.
As a side note, the pages are a mix of HTML and PHP. The HTML is supplied to me and I've been adding the PHP in to add functionality to fields and to get data from a database. I have no problem using PHP to echo the HTML links if I have to, and if it can be done in HTML with a small <?php someCode(); ?>, that's fine too.
I thank you for your time to try and help me out.
EDIT:
So to try and clarify a bit, I have a structure that is currently tracking pages that the user has already been to as they visit them. It creates a mini history of the pages. My issue is that I have code like this:
Back To Previous Page
And I don't know what "somelink" is since it will change depending on your history. I know I can do something like:
Back To Previous Page
If I do anything like the above, the function is executed as the page is being displayed, so it makes it difficult to use an array_pop() or a dequeue() but again, the PHP will be executed as soon as the page is displayed. What I'm looking for is a way to display the link and then remove it from the history if and only if the "Back to Previous Page" link is clicked. As of right now, I'm storing an array in $_SESSION as was suggested below and since it's an array, I can show the last element in the array as the link, so the only real problem is to find a way to remove elements from the array when the link is clicked.
EDIT 2:
I've been scouring the internet and decided upon using JavaScript with AJAX to call a PHP file. This allows me to us an onClick on the links I have so that I can control when I array_pop from my $_SESSION['links'] variable.
I don't think my AJAX is actually doing anything sadly, so the code I'm using is below.
<script src="js/jquery-1.4.2.min.js" type="text/javascript">
function dequeue()
{
$.ajax({
type: "POST",
url: "common.php",
data: {action: "rem"},
success: function(output) {
alert(output);
}
});
}
</script>
and the PHP is
switch($_POST['action'])
{
case "rem":
array_pop($_SESSION['links']);
break;
default:
if(isset($_SESSION['current']) && $_SESSION['current'] != $_SERVER['REQUEST_URI'])
{
array_push($_SESSION['links'], $_SESSION['current']);
}
$_SESSION['current'] = $_SERVER['REQUEST_URI'];
break;
}
As far as I can tell, this will allow me to add a link to the history in the session variable unless I'm clicking on the "Back to Previous Page" link since that link will have the "rem" code. I'm also a bit suspicious of the $_SESSION['current'] = $_SERVER['REQUEST_URI']; and where it should be placed.
You can store array in a session and treat the array like a stack (use array_push and array_pop accordingly). When the user hits something but the back button, push the current page to the stack. otherwise, pop it.
I would do it like this if I had to:
$_SESSION["history"] = array();
And within the header of every "rememberable" page:
if(in_array($this_page, $_SESSION["history"])) {
unset($_SESSION["history"][array_search($this_page, $_SESSION["history"])]);
}
array_push($_SESSION["history"], $this_page);
What this does is: "If the page exists in the history, remove it from wherever it is and put it as the last page of the history. If not, just put it as the last page of the history". That way you won't have any loops.

Browser Back Button Ajax

I know there a bunch of topics on this, but I couldn't determine what to do based on what I read in the other topics.
I have a page "abc.php". The user can do a search which then populates a form with 2 ajax requests. Then if the user navigates to another page and then clicks BACK to "abc.php", the contents of the form is not complete because the ajax doesn't run. Is there a way to make this happen?
Modify your URL when you do the ajax by adding the search terms there after a hash (e.g. http://example.com/search.php#search-terms-here)
Then when the page is loaded, read the search terms back from the URL.
This is a very nice article / tutorial on enabling the back-button using jQuery.
Using history.js, the following function 'listens' to changes in the url bar, and calls a function to load the appropriate page:
History.Adapter.bind(window,'statechange',function(){
var State = History.getState();
page(State.url);
});
function page(url) {
//AJAX
}
Now whenever you want to change the page, you call:
History.pushState({state:X}, "Page Title", "Page Url");
This will update the browser's url bar, and automatically call page(State.url) for the new url; and all the browser features like forward/back button, bookmarks, etc... should work.

Categories