Any way to change the header URL without reloading? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Modify the URL without reloading the page
Updating address bar with new URL without hash or reloading the page
my site is completely based on Ajax requests and simple navigation within the site is done from only one single page. This means when users click the menu items it doesn't load another page rather it loads the content inside an HTML element. But when a user wants to link a page to their friend it will always be the same because http://mysite.com/mymainpage.php will always be the same regardless of what ajax pulls up. How can I modify the users header? For instance when they click on "games" can jquery/js/php/html change the current URL address bar on the browser? so that it becomes http://mysite.com/mymainpage.php?games=true. Then the user can just copy the sites url from the address bar and it will lead to the correct section for ajax to load up.

You can use hashes:
http://mysite.com/mymainpage.php
http://mysite.com/mymainpage.php#games
(That's what gmail does: https://mail.google.com/mail/#inbox, https://mail.google.com/mail/#sent)
You can read the hash using
window.location.hash /* gives "#games" */
or
window.location.hash.substring(1) /* gives "games" */
And you can change it using
window.location.hash="#games"
or
window.location.hash="games"
(it seems it works but better use the first option)
Then, each time user clicks a link, change the hash and load the new page like you do now.
And when the user enters to your page, check if there is a hash in the URL and load that page.
You could also add a hashchange event listener. See https://developer.mozilla.org/en-US/docs/Mozilla_event_reference/hashchange
Edit:
No, you can't have two hashes ( http://mysite.com/mymainpage.php#games#fashion )
So if an user goes to http://mysite.com/mymainpage.php?games=true, you should redirect him to http://mysite.com/mymainpage.php#games.
And then you can change it to http://mysite.com/mymainpage.php#fashion without reloading.

Related

I want to click on link and not go to other html page but echo a page. Is that posible in php

How can I use php to echo a page instead of linking to existing html page with hyperlink?
One example would be
<html>
<body>
click on this link to go back
</body>
</html>
Now, I don't want this link above to be a link to html page but to echo a page with php code when user clicks on click on this link to go back(to generate a page). This way, nobody can access a page after they logout.
Can php do this?
If someone logged out of your website or application I assume you will have a check whether or not this person is allowed to view the content.
Your question itself is very unclear to me. But it sound a bit if you want to do client-side coding (don't follow a link when it's clicked) with PHP which is not possible since PHP is a server side language. You will need Javascript to change the behavior of a link (for example, make an AJAX request which returns the content of another page).
Create a function, what the function should do is it should get triggered on a button click event and the code inside the function must send an curl request to the url you want and get back the html from it and echo it on your page
For answering the second part of your question!. you want no one to access the data without logging in so maintain $_SERVER['']; and sessions for users and validate if the user is inside a genuine session then show him content else no

PHP MVC Append URL parameters without page reload?

I am working on a site which has an animated background, the animation is powered by javascript and therefore when the user enters and submits a search it would be great if the animation were to continue smoothly whist the search was completed and results appended to the DOM.
I have managed to get to the point where the results of the search are returned to JS from an ajax request although inherently the url of the ajax request is different from the url currently displayed in browser.
So my goal is for the user to come onto the site at say, www.example.com/public/home/search
They type something into text input and press search, the url changes to something like
www.example.com/public/home/search?q=some+search+query or
www.example.com/public/home/search/somesearchquery or
www.example.com/public/home/search/#somesearchquery, etc.
but the page state remains the same, the results are appended to the DOM and no full reload occurs.
Returning to a url like the one above should load the page and send the query automatically, returning the page with the results already appended.
I don't know weather this is possible, with or without obeying the MVC pattern.
I am not using any mvc framework and would like to avoid it if I can but instead using a bare bones system similar to the one found at. https://www.youtube.com/watch?v=OsCTzGASImQ
Any ideas, suggestions, alternatives?
There are several answers here for "change URL without reload", for example: Modify the URL without reloading the page .
So, I think you just have to implement some solution from one of these answers and run some javascript that change the page.
You have to be careful, because the modified URL must to load the same version of the page the user is seeing after the changes caused by the javascript. Otherwise, if the user copy and paste the URL, he will not be happy.
One way to archive it is to create the javascript function that "updates" the page without reload it based on the text input (let's say, f). Then, if the user try to access directly the page
www.example.com/public/home/search?q=some+search+query
your server side code just return the page search with a call to this javascript function at the end, like that:
f("some search query")
so, this function will update the page and the final effect will be the same as the user just enter in the page and after tries to type some search query.
(Note that, this function f may be used in both cases: when users type the text to search and when users just paste the entirely URL).

Open jQuery dialog from previous page

This may sound vague, I apologise for that. But I can't seem to find anything or anyone that's trying to do the same as me.
Although, I've just seen How to trigger open a jQuery UI dialog from a separate page? but I'm not sure that would strictly work.
I have a single profile page for members with the data driven by an XML feed. On the profile page is a link that opens a jQuery dialog box. This is working fine.
Elsewhere on the site, is another page that generates a list of members depending on a filter, with a link to that users profile. Also on this other page, with the list of members, is a duplicate link to the jQuery dialog box.
How can I make this duplicate link go to the profile page and automatically fire the jQuery dialog box to open?
My way to do this is to use Hash part of URL
for example your URL to profile from other page should be like this
profile#showdlg
and in profile page
var hash = window.location.hash.substr(1);
if(hash == "showdlg"){
//Show dialog here
}
And this should do the trick
You cannot (should not) directly trigger some script action in a page "to be loaded in future". Instead the trigger should be part of the page itself.
So if that profile page is generated in a dynamical way an approach would be to implement a conditional feature that adds such trigger (like using jquery to fire the dialog when the dom tree is ready, there are millions of examples for that). The condition would be whether the profile page has been called via such a special reference or not. You could detect that by looking at the HTTP-REFERER. So it boils down to: if called in a specific way, then add a 2-lines-of-code trigger to the profile page that initially fires the dialog.
To answer your comment below here some more detailed description:
There is not much coding involved. The links reference the users profile pages. The profile pages are generated by php I assume. So all you need to add is one detail: inside php check if the request currently processed has a certain referer it was raised from:
<?php .... if ('other_page.php'==$_SERVER[HTTP_REFERER]) { ... } ... ?>
If so you know that the profile page was called from that other page instead of the normal situation, so you want the UI dialog to fire by itself. For this you add a tiny javascript to the generated page which does the trick as soon as the page has loaded:
<script>$(document).ready(function(){$('#mydialog').raise();})</script>
The details obviously depend on what type of dialog and how it is raised. But you should get the idea of what I suggest...

Reload previous link on click-event of back button of browser? [duplicate]

This question already has answers here:
How to detect the back button
(2 answers)
Closed 9 years ago.
i am developing one website in which main content is getting load by jquery ajax according to selected menu item.
on selecting any menu item, url get changed according to this pattern:
http://host/domain/index.php?pageid=page
here page refers to the page that i want to load into main content using ajax.
now in this case, i want to reload the previous page if user clicks on back button of browser.
can anyone help me out how could i achieve this?
If you do not change a url, then I'm afraid that is not possible.
Suppose you should better try to use window.history which provides onpopstate event when user clicks back button. But you will need to modify url in browser with history.pushState/history.replaceState functions. Possibly, you can add same url to history, so it will not be changed visually. And then take previous URL from your custom history array. But not sure if popstate will work if you place same url with pushState
But that will work in modern browsers only. To make it work in all browsers, you should better use some history plugin (for example this) which will also handle IE using hashtags

PHP, Returning to a calling Page after saving a record in a Edit page

Lets say I have a Page with a List (list.php).
I click on a row on that list to Edit that record. I go to a edit.php Page.
I have 3 buttons on that edit.php page. Save, Apply, Cancel
Save button - Saves the Record and returns to the (list.php) Page
Apply button - Saves Record but stays on the same page (edit.php)
Cancel button - No save, just return to the (list.php) Page
But now image if I can access for edit that item on a different page. How do I return to that calling page?
Do I add a parameter(code) to the URL? something like a Page Origination Code?
Do I save the previous page URL in a session? (bad, they can right click open another page and that would be saved to session url)
Am just curious to how others return to a previous page after a SAVE.
you can the server variable $_SERVER['HTTP_referrer'].
They are other ways also you can store in session the current page and use is processing page.
Adding a parameter to the URL is the only reliable though quote ugly way.
That's why such an in-place editions nowadays often being implemented using AJAX, and this very site is a perfect example.
However, there are different cases.
Login page is imperfect example for example, as you always have a form instead of just a link, and thus you can always store the current page in a hidden form field.
Another approach is possible if you are using some sort of front controller, and all requests actually being directed to the single index.php file which runs appropriate script based on the URI.
in this latter case you will need no more than mere a redirect to the current page.

Categories