Basically i have a favorite icon on the users profile page. Another user can press this button and it will link to favorites.php where it will carry out the sql query to add that user to the database.
This then leaves the user stuck on favorites.php faced with a blank page. What i want favorites.php to do is after its processed the query is echo out a piece of text that says user added to favorites on the previous page profile.php. but i can't simply redirect them to profile.php using header because each user profile has an id extension like profile.php?id=13 and they will have clicked on that users profile.
so my question is can i use a header to redirect to the previous page they was on (url specific) so that its that users id they was originally onwith that corresponding . can this be done?
Thanks
This sort of UI interaction is typically accomplished with AJAX calls these days.
When the user clicks on the favorite icon, a bit of javascript on that page would call favorites.php in the background. Favorites.php would then issue the SQL call and return a bit of json (using json_encode()) to tell your Javascript code whether or not the SQL was successful. Your javascript would then react and update the UI accordingly.
jQuery is a very common way to accomplish this, so I'd suggest a quick google for "jquery ajax tutorial".
If you absolutely must support browsers which don't have javascript, the alternative would be for favorites.php to look at the referer:
<?php
//do important stuff here
http_redirect($_SERVER['HTTP_REFERER']);
?>
However, php's manual indicates that HTTP_REFERER is not reliable, so you still may end up with errors. Ajax for the win.
Related
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
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...
I am a newbie to programming. I have a PHP website which works as follows
Index Page - Search Results - Show a Product
The site user enters search critera on Index Page and the page is POSTed to Search Results page. From there, the site user clicks on a Product href that takes him to the Product Details. This is working fine till here.
The problem occurs when the user click the browser BACK button. The Search Result page comes up totally crashed and the user has to press F5/Browser Refresh to re-submit it. Any idea/technqiue that I can use to avoid this crash?
When a browser goes back to a page that comes from POSTing some data, the browser often times needs to re-POST the data in order to get the same page back. Since that can sometimes be bad (e.g. re-POSTing an order form), many browsers require the user to force a refresh with a warning.
You can generally use a GET instead of a POST form to avoid this.
An idea would be using GET for the method of your search form instead of POST (that apparently you are using). That way, even if going back in browser history, your server could re-supply its search results.
You would need the following:
change method="post" to method="get" in your search form
change every $_POST relating to the search form data to $_GET in your search form processing php file.
Of course, it could not work for your specific usecase. That's just an idea.
I have a classifieds website.
In every classified, there is a back link which simply takes the browser back one step.
This is because when users search classifieds, and click on one to view it, they can easily go back with a link also (instead of only the browser back button).
Here is the problem, if the classified is entered directly into the adress bar of a browser, or if somebody bookmarked a classified, then this back-link would take them someplace else...
Is there any way of making sure that the previous page is a certain page (index.php in my case)?
This way I would only display the back link if the previous page was index.php...
Thanks
You can't query history data. A slightly better option is to read the Referrer server variable and create your "Back" link to it. It's not very much stronger than history.go(), though. Try using a common index page instead.
Why not just insert a link to index.php directly? That way you have complete control over the target of the link. No need for JavaScript.
You should track their session within PHP or whatever language you're using in order to have an effective "back" button.
You could pull the data with document.referrer but that will not always give you the previous page. Sadly I am not sure of a way to achieve a "Back" button via javascript without using some kind of scripting language to track a user either via cookie or session.
Is it possible to detect when the user clicks on the browser's back button?
I have an Ajax application and if I can detect when the user clicks on the back button I can display the appropriate data back
Any solution using PHP, JavaScript is preferable. Hell a solution in any language is fine, just need something that I can translate to PHP/JavaScript
Edit: Cut and paste from below:
Wow, all excellent answers. I'd like to use Yahoo but I already use Prototype and Scriptaculous libraries and don't want to add more ajax libraries. But it uses iFrames which gives me a good pointer to write my own code.
One of my favorite frameworks for doing this is Yahoo!'s Browser History Manager. You register events and it calls you back when the user returns Back to that state. And if you want to learn how it works, here's a fun blog entry about the decisions Yahoo! made when designing it.
There are multiple ways of doing it, though some will only work in certain browsers. One that I know off the top of my head is to embed a tiny near-invisible iframe on the page. When the user hits the back button the iframe is navigated back which you can detect and then update your page. Here is another solution.
You might also want to go view source on something like gmail and see how they do it.
Here's a library for the sort of thing you're looking for by the way
There's no way to tell when a user clicks the back button of presses the backspace key to go back in the browser, however there are other events that happen in a certain order which are detectable. This example javascript has a reasonably good method for detecting back commands:
The traditional way, however, is to track user movement through your site using cookies or referrer pages. When the user goes to page A, then page B, then appears at page A again (especially when there's no link on B to A) then you know they went back - A can detect this and redirect them or otherwise.
The Yahoo User Interface Library, my personal favorite client-side JS library, has an excellent Browser History Manager that does exactly what you're asking for.
The simplest way to check if you came back to a cached version of your page, which needs to be refreshed, is to add a hidden input element that will be cached, and you can check if it still has its default value.
Just place the following inside your body tag. I place mine right before the end tag.
<input type="hidden" id="needs-refresh" value="no">
<script>
onload=function(){
var e = document.getElementById("needs-refresh");
if (e.value === "yes")
location.reload();
e.value = "yes";
}
</script>
I set a variable $wasPosted in $_SESSION with value false.
All my posts go via the same php file, and set $wasPosted to true.
All header(location:) requests are preceded by setting $wasPosted to true.
If $wasPosted is false then the page was loaded after use of the backward or forward buttons.
The dojo toolkit has functionality to deal with this in javascript. I don't think there is any good way to handle it in pure PHP.
Here is the docs page they have: http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/back-button-undo