Sending information across pages PHP - php

i have an ajax / php feature of my site that pulls in information from a table every 20 seconds. The information is about hobbie's
It displays on the page the name of the person and their hobby. I have a second page that acts as an information source for that hobby. Currently the only way to access the second page is by entering your hobby into a form on the first page. How would i get it so that i could click on the hobby that is being displayed on the homepage and access an information page on the hobby.
To access the information it currently grabs the hobby from a POST command.
The table results are being displayed on the homepage via
echo $row['name']." is interested in ".$row['hobby'];
Could i some how pass the hobby name through to another page? I only know how to do it through form submits.

I don't think that sesssions are necessary for this.
Look at what information your form is sending. If you're making a GET request you can add that url to a link.
So on your home page you could have links like the one below instead of forms.
<a href="/hobbies/?name=remote+controlled+cars>Remote Controlled Cars</a>
So with the link it would no longer be using $_POST but $_GET instead.
Psudeo code for your homepage link below.
<a href="/hobbies/?name=<?php echo slugify($row['hobby']); ?>><?php echo $row['hobby']; ?></a>

Look into using PHP's sessions.

You can create an anchor tag that is generated from the data provided with a query tailing it to carry the data over, effectively creating a link pointing to the next step with additional information. The only problem, of course, is that this is exposed to the user which can be hijacked.
You can also, if you are using only form submits, create a <input type="hidden"> with a value/name of something you can use to navigate to the next page, if you so desire.

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

Echo out text on previous page after link?

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.

PHP - Browser BACK button crashes the website

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.

Keeping track of more than one level of page referrers

The scenario (all happening within the administration area/backend):
From the listing page, the user clicks a link to view an article (on the backend).
From the article view page, the user clicks a link to edit that article.
In the article edit page, form is submitted to the current uri.
If validation succeeds or user cancels, user is redirected to the article view page.
From the article view page, the user click a 'back' link to return to the listing page.
List <--> View <--> Edit
Right now, I'm only able to track referring url from a previous page. In the edit form, I'm using a hidden field to maintain referral to the view page, lest it be changed during failed form POST submission to itself and user remains in the edit page.
Problem is that when the user returns to the view page from edit, the 'back' link to the listing page is now linked to the edit page.
FYI,
The listing page url is dynamic as the user should return to the listing on the same page and sort order (stored in query strings); therefore a fixed url is out of the question.
In the past, I've tried using sessions (e.g. SESSION['view_to_list_ref'] SESSION['edit_to_view_ref']), but it messed up with multiple tabs.
I could transition between view/edit via ajax, but I'm hoping to keep the app simple and ajaxless at this point of time.
I'm using PHP + Kohana 3.2 Framework
The only solution I can think of is to have the list page url encoded and appended to the 'view article' link via query string. This way, the location of the listing page is preserved even while in the edit page; as the referring url back to view page would also contain the listing page url in the query string. However I don't really like the idea of 'dirtying' the url with long parameter values (encoded or not).
I'm really hoping there is a more elegant solution to this problem of generally tracking multiple levels of page referrals; not just specifically to solving the scenario I've mentioned.
EDIT: Oh and the solution should be able to support multiple tabs performing the same scenario.
You could track the pages by using a unique identifying code in a PHP session, a temporary variable, and using a temporary database table that tracks page loads by these temporary values.
The database structure might be:
+-------------+-------------------+---------------------+
| Unique ID | Page Referral | Time of page load |
+-------------+-------------------+---------------------+
Tracking time of page load would allow you to selectively wipe loads older than X minutes, and keep the table relatively small.
Anyway, this would allow you to keep as many levels as you'd like, and if you wanted to add an auto incrementing counter field, or your own counter field, you could even keep a simple to use number system that tracks page loads, though I believe the time of page load would suffice for that scenario.

Store different data to each tab

I need store different post data to each tab in a browser. If I open a new instance of the same page, the data aren't shared between both.
My problem:
I'm building a CMS to control my website content. But I will open some instances of the same page (many tabs). So I have a search form to find news that I been created on my CMS. If I open a news item I have a cancel button that back to previous page (the news list).
The problem is that the news list have a pagination and a filter form. So I can, for instance, search by a term like "john doe" and advance to page 5, and open a news item. If I cancel, currently I back to news list without filter and on first page.
My solutions:
Well, I don't want to use the history.back() because I can submit a news form and click on back/cancel button. So, I'll back to the current form, what is wrong.
My second idea is to store a $_SESSION with the $_POST sent to the news list and the back button send me to /news/list/recovery-session, that will recovery the $_POST data from session. But it have a problem: if I open two tabs and make two searchs, I'll have only the last session saved.
Your solutions:
Well, I can work with PHP and JS to make it work. You can suggest a idea of what I can do. I think about work with COOKIES, but I belive that it is shared by domain, and not by tab, what is a problem.
Someone?
Generate a unique id and attach it to the form or some hidden element that will be submitted. Save that unique id in a cookie or session variable. Compare the two at time of submission.
If second tab has generated a new id, the first tab will not evaluate to true.
The main problem is the need to persist the state of what page to return to and also the search term when returning back to the news list page. There are many ways to accomplish this, but one simple method is by encoding this data into your query string.
As an example, assuming your search term is "john doe" and you are on page 5, pass that data along to your news page.
news.php?returnSearchTerm=john+doe&returnPage=5& ....
When the news page is created, you can format your cancel link to send you back to the list page with the correct parameters.
news_list.php?search=john+doe&page=5

Categories