PHP passing URL parameters best practice - php

I have a search page that outputs search results. You can filter results based on on the following criteria before running the search.
1. status
2. order by
3. record type
Viewing a record redirects the user to the edit form page and if they click the cancel button they redirect back to the search results.
Here's what I've done:
I stored the GET parameters (status,order_by and record_type) in session variables
When the user clicks cancel on the edit form, I use the session variables to redirect back to the search results as follows:
if (#$_POST['cancelbtn']){
if (isset($_SESSION['searchForm'])){
header("location:searchForm.php?products=".$_SESSION['product']."&ticket_status=".$_SESSION['status']."&order_by=".$_SESSION['order_by']."&record_type=".$_SESSION['record_type']."&searchbtn=Go!");
}
}
The above works for me but I'd like to know if this is the best way to do it? Thanks.

It's not a bad way of doing it, but I would try and future proof it a bit. Instead of storing each individual variable in the session, try storing the URL. That way, if you change your search form to have some additional parameters, you won't need to change much of your code.
//Your search results page
$_SESSION['searchResults'] = $_SERVER['REQUEST_URI'];
//Your form page
if (array_key_exists('cancelButton', $_POST) {
header('Location: '.$_SESSION['searchResults']);
exit;
}
Hope this helps

Related

Ajax $_get search terms lost after user clicks on result

Ive got a member search form that searches users on my site. The form method is get.
The results are displayed using Ajax on the same page. The page is not reloaded.
If a user clicks on a search result, then the appropriate page is opened in the same tab.
The problem comes when a user clicks back. Because the search result has opened in the same tab, clicking back returns you to the page with the search form, but all search results are lost. The user therefore has to start the search again.
The only solution i can think of is to not use ajax and post the search terms to the url. But thats not ideal.
Anyone got any better solutions - somehow storing the get variable so that ajax can pick it up again?
I think you used 'history.back()' for back button. You should use get or post to pass inputed variable of user to form search.

Redirecting on a single search result?

When a user does a search on my website, and there is only one entry, then I want to redirect the user to the search result. The current way that I am doing this is poor. Here is how I am currently doing this:
If there is only one search result on the search result page, then I render a hidden input with an ID of "redirect" and a value of the link to redirect to. In the javascript, if the hidden input with ID "redirect" exists, then the user is redirected to the value of the element.
It's a poor way to do this because the single search result is loaded first, so the user actually sees that there is one search result. Then, it loads after, say, 3 seconds.
Is there a better way to do this?
You could use the header() PHP function to redirect if there is only 1 result.
header("Location: http://www.example.com/");
You should use PHP to determine if there is only one result, then do a server-side redirect like so:
header( 'Location: http://www.yoursite.com/redirect_page.html' );
exit;
Before printing anything to the page, check to see if there is only one result, and if so, render that script and exit so that nothing else gets processed.
<?php
//Query & other stuff
$num_of_results=mysql_num_rows($result);
if($num_of_results==0){
//no results
}elseif($num_of_results==1){
//only 1 result
//pseudo-code
//get id or controller ect from result set
header('Location: ./page/'.$id);
die();
}else{
//normal display of search results
while($row=.......){
}
}
?>
The issue with your logic is that you're waiting for something in the page to load THEN redirecting. I think a more elegant solution is to change the flow of things to give you a little more flexibility.
First, you're going to want to preprocess your query and check for the pertinent information; if there is one result, use the header(); redirects as mentioned. You may need to add some more information to the result set (database table) to make this possible.
I think taking this a step further, however, would be to redirect certain terms automatically as well. You'll kill two birds with one stone.
Let's say you have a database that is term and url - you could add certain terms to the list that also serve as a redirect. This is great for certain keywords that there are variations of. Use this sparingly though - it is great to use in conjunction with your site statistics. It may help you in instances where 0 records are shown.

PHP refresh, how can I stop the previous function being called again on page refresh?

I have a function that imports a files contents into mySQL and returns the results. If i refresh the page and click "yes" it will do it again doubling the output with the same content.
How can I stop this happening? In this particular case there is no URI in the address bar but on other functions there is.
You should just check if the contents in the database exist, if they don't, fill them. Otherwise don't run the function.
psuedo-code:
if !database.containsRecords
fillDatabase()
end
On top of this, it is always good practice to redirect after a POST request. So you would want:
fillDatabase();
header("Location page.php");
exit();
Query the database on each page load and see if it has already been populated. If it has been populated then don't attempt to populate it again.
You should use the POST-redirect-GET pattern.
After updating the database, send an HTTP redirect to a separate page that displays the results.
Refreshing the browser will reload that separate page.
I assume you use a POST form to upload the file.
You can include a hidden input field with an pseudo random unique id in your form.
If the user resends the data via POST you can check if you already processed this request.
Hidden fields are not save because a user might edit them, but you can detect accidental resubmits.

How to display results so back button does not ask to reload POST variables

I have a form that uses XML to get results. From those results users can click to a detail page. My problem is when a user clicks back to the results page, they are asked if they want to submit the form again. How do I create this so back button just displays the results, like on aa.com, ebay, autotrader, etc.
Thanks!
When you submit your page move the $_POST variables into the $_SESSION array and then header redirect the user to the results page.
You should redirect to another page to using redirect() method of codeigniter. This will prevent the browser asking a confirmation on form submission.
Is it just a search page that displays results? Why not use GET rather than POST in your form? Looking at search engines out there, they seem to use GET for their search interface. I can think of a few reasons to use GET rather than POST.
If the operation simply fetches results, semantically, the GET method is more appropriate. GET is used when you are fetching data. POST is more used when you are submitting a change to the application.
If you use GET, clicking on the back button won't give you a dialog asking whether you wish to resubmit the form.
Your users will have a URL directly to a search results page for a particular query that they can share.
Unfortunately CodeIgniter, by default, nukes the query string when processing a request. You can enable the query string in CodeIgniter by following this answer.

PHP: Pass non-form variables between pages?

I have a page. The user submits the page and sends it to a PHP results page. It works fine. Now, I want the results page to link to another page, and for the elements on that page to depend on what was on the results page. I know how to pass form variables to another page, but I don't know anything about passing non-form variables.
From my searching on the web, my best guess is that I should be passing by URL. Is this correct? If so, a possible problem: the page I want the results page to pass to will have a form, and the user will go to yet another results page by clicking submit (the form data will be sent by POST). Can I send the non-form data (the old results page variable) along with the form data, if the user is going to the other page using POST?
I strongly suggest using sessions. It's not that hard to learn, php makes it VERY easy using http://php.net/session_start and the $_SESSION variable.
Advantage is that you will not have to submit a form on every click, and no information will be displayed in plain text in the URL.
There are several options. However, the easiest may be to simply pass the data on using hidden input fields.
Other options would be using the session, storing to a database between forms, or some combination therein.
If you are going to use POST to go to the next page the most simple option is to include the data you want to send along using an input type="hidden" element in your form.
You might consider using a session to pass the data along.
You can embed the non-form data into the second form as hidden fields. To make sure that it is also passed in the link, you can just add it to the URL as a query string, like:
http://..../blah.php?var1=val1&var2=val2
as long as the data you're passing can fit into a URL. Be sure to urlencode() anything you're appending to the URL.
<?php
start_session();
$_SESSION['Foo'] = 'Bar' // Add stuff here.
?>

Categories