forwarding url parameters to the next url - php

Using PHP (for the first time in my life) and working in a CMS environment without access to the back-end PHP pages or code, I created a form on one page that places four parameters into the url of the next page to which the form send its data.
Here is the form page:
http://CMSDetroit.org/480
Here is the url the form generates:
http://www.chambermusicdetroit.org/422?School=formdata&Grade=formdata&Teacherformdata=&Handle=formdata
I need to collect the parameter information from the url and pass it on to the url of the next page after this one, either by putting a command into every link on the page (422), or through some other more efficient method.
I've tried all sorts of things and keep coming up dry...help!?!

For PHP see $_SERVER and more specifically QUERY_STRING
Then with that I'm not sure how you are invoking the next page? maybe with header?
header('Location: http://www.example.com/?' . $_SERVER['QUERY_STRING');
exit;
Point being it is just one large string. If you don't know already you use $_GET and key to extract the individual values or if you want you can use explode on the QUERY STRING

Related

Change form submission from GET to POST PHP

I'm working on big site and it has filtering of cars in it. I'll explain how the form filtering works:
so the user chooses filter options, car number and so and presses go, which makes GET request to server (php) i've changed every variable from $_GET to $_POST and changed form submission method, the problem is that when form returns big number of items it might have more than 2 pages, so when the user clicks on second or third page all this code does is add page number to the request like this: www.example.com/GET_REQUEST_VARIABLES -> www.example.com/pagenumber&GET_REQUEST_VARIABLES. that way server returns second page items and so.
but when i send post request, it isn't saved in the url so server doesn't know what to return,
can you help me solve this problem?
i can explain better if you ask questions i don't know if i explained clearly here
Is there a reason you are using POST instead of GET? Using GET is more SEO friendly in this case. And since you aren't sending sensitive data in the form submission, I don't see a reason to use POST. Another advantage of using GET is that you can directly link to search results. (For instance, if I search for a Honda Fit, and want to show my wife, I have a direct link to the page)
(Look HERE for a bullet-point explination of the difference bewteen POST and GET).
To answer your other question, POST does not use the URL. Only GET does So if you need the form data to be serialized into the URL, you'll need to use GET. Since you changed the form submission method to POST, you'll need to change the server side logic as well to accommodate. I can't be of much use without knowing what server-side technology you are using. Assuming you are using PHP, you could start here
If you aren't much for MAN pages, just change all the $_GET['fieldName'] calls in PHP to $_POST['fieldName']
You could always POST the first page, and use GET ONLY for the page number. In the form you would just do this
<form method="post" action="www.foo.com/search?page=1">
Then you can store your POSTed variables into the $_SESSION super-global. BOOM. POSTed pagination. It would be a breeze to render it on the fly with PHP
$nextPageURL = 'www.foo.com/search?page=' . $_GET['page'] + 1;
if($_GET['page'] !== '0'){
$lastPageURL = 'www.foo.com/search?page=' . $_GET['page'] - 1;
}

# in query string breaks php GET

Running into a problem with a form I have built. A user typed in a hash (#) for their address in the address field. When their address is put into a query string after the form is submitted all of my GET variables are broke after the field with the #. Anyone know why this would happen? Do I need to convert the # to a different character or use some other type of encoding? This is the code I have on the page that grabs the values from the query string..
<?php echo $_GET["address"]; ?><br/> // if this field has a # in the value, the GET variables below do not work..
<?php echo $_GET["city"]; ?><br/>
<?php echo $_GET["state"]; ?>
A # is a valid anchor symbol in URL and only has meaning to the client browser.
In all likelihood you should probably be using a POST for this form rather than a GET, at which point this issue goes away.
Usually a good rule of thumb is to use URL parameters (GET) for cases where you want the URL to be navigable by any user and where such navigation will not change any data on the server (or do things like trigger emails, etc.). A good example of this might be the use of product ID's in the URL for a e-commerce catalog application to determine what product to show on a page.
You should typically use POST for cases where you are going to change data on the server or trigger some action which basic navigation to a page should not trigger. Building on the earlier example of an e-commerce app, you might typically use POST to add an item to a users shopping cart.
get parameter should be url endcoded http://www.php.net/manual/en/function.urlencode.php another option would be to build your query string with http_build_query (http://php.net/manual/en/function.http-build-query.php)
The hash in a url is a document fragment and read by the browser. Hence anything after the hash sign in a url will not be transferred to the server as a query. You need to URL encode the hash (by javascript i presume). See :http://www.w3schools.com/tags/ref_urlencode.asp

Pagination together with a html form

On my website I use a pagination (similar to that one on the bottom of this page) for MySQL output. For the change of the current page I use GET method (variable page) and it works well.
However, on my page I have also a form, using method POST, which acts as a filter for the MySQL output. This rises a problem because, when I change the form settings an submit them (POST), the page in the address line (GET) remains the same. This is problem in some cases when the filtered output has less pages than that one currently set.
Is it somehow possible to set the page variable to 0 always when the form is submitted?
Particularly, I did it using $_SERVER['REQUEST_METHOD'] == 'POST'. However, this changes just the variable in the code. Not at the address line.
On the other hand I want to keep the POST variable when I change the page of the output.
Thanks in advance.
There is a logical collision in your setup:
Unlike GET method, POST method doesn't keep variables in the address bar. But for some reason you are using POST method.
So, the solution is quite simple - use GET method for the filtering.
To create pagination links use http_build_query() out of $_GET array
In fact it's better to send filter criteria in the address (GET method). It will solve your problem with pagination and you (and your users) will have a direct link to search results.
In your PHP code, you want to redirect to the URL with the page GET parameter removed if you are SUBMITTING (by pressing the "Filter" button, or whatever it's called). So it will start at page 0.
You would have to rewrite the URL yourself (or using a plugin).
Typo corrected, I meant page 0, not 1 :P

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.
?>

Back button re-submit form data ($_POST)

My problem is that the back button causes the browser to say something like "Page expired" when the previous page was created by a form.
Example:
page1: form submitted with search
criterias ($_POST request, form
points to page2)
page2: Receives $_POST request and
show result (list of user with links,
points to page3)
page3: Show user profile
Now when the visitor clicks the back button in the browser it will show something like "Page expired".
Instead the previous page should be shown with no warnings (page2, with the userlist)
How are your strategies to get around this behavior?
If you are submitting a form with search parameters, you are trying to get some data, not modify some.
So, you should use the HTTP GET method, and not POST : POST should be used when you intend to create/modify data, and GET should be used when you intend to fetch some data.
Or, if you have some create/modify operation that has to be done :
The form first POSTs to a first page
That page does some operations (like writing something to a database)
And then redirects to another page, using a Location HTTP header.
It's that last page, that's queries by the browser using a GET requests, that displays the data fetched from the parameters received in the URL.
See the Post/Redirect/Get page on wikipedia, about this.
Use the Post/Redirect/Get (PRG) Pattern.
This applies to PHP and IE8.
Not only must you set cacheing to private, but you must remove the 4 cacheing headers and this can only be done with PHP 5.3. In PHP 5.2 you can only set the 4 headers to blank values if using the Zend Framework's setHeader() method. For some reason is not sufficient on IE8 to set the 4 header values to empty values. Here's the code for PHP 5.3:
header_remove("Expires");
header_remove("Cache-Control");
header_remove("Pragma");
header_remove("Last-Modified");
Send a Location header in the script you POSTed to, pointing to the page that comes after.
Don't use POST for search. Search can safely be done with GET since it won't alter anything.
You can use session to do this.
eg.
$_SESSION['name'] = $_POST['name'];
Remember to unset your variables after the process is complete to optimize memory usage.

Categories