nikagar4 nikagar4 - 2 months ago 7
PHP Question

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

Answer

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;
}
Comments