Form action where destination is one of two pages

Hi all. I currently have my web app programmed such that a form is submitted via GET method to submit.php, where submit.php then determines whether (count($selections) == 1) or (count($selections) > 1) . If 1 product is selected, submit.php processes one set of code, and if 2+ products are selected, submit.php processes a different set of code.

This causes the obvious problem that the page is the same even though it appears very different depending on how many products are submitted, i.e. submit.php?selections=productA looks like an entirely different page than submit.php?selections=productA+productB

What is the proper technique for dealing with this situation? e.g., should the form submit via POST method to submit.php, which then sends the user via 301 redirect to either /oneproduct.php?selection=productA or /severalproducts.php?selections=productA+productB

Thank you!

If it’s a php script, it’s not necessarily tied to any particular “page”, the html output can be whatever you want according to any variable.
For example:-

if(count($selections) == 1) {
    include $_SERVER["DOCUMENT_ROOT"] . "/includes/html-temps/one-product.php";
else {
    include $_SERVER["DOCUMENT_ROOT"] . "/includes/html-temps/many-products.php";

Moved to PHP.

Yes, thank you. I may not have been clear enough. The problem is not about the code organization as much as the URL. If a user selects 1 product, I want them to go to, for example, /product/apple-watch/, and if a user selects 2+ products, I want them to go to, for example, /comparison/apple-watch-vs-fitbit-surge/. I plan to use htaccess to change the URLs, but I don’t know much about htaccess logic and figured I should handle the logic on the PHP end. i.e. have the user actually be on different .php pages.

Yes, that would be a good technique except you don’t use 301 but 303 status code (See Other) for redirection after POST.

Additionally, you could duplicate the same redirection logic with javascript, in which case the people who have javascript enabled don’t have to be redirected via your php script - they will go straight to the target page saving them a fraction of a second in speed and saving you a few server CPU cycles.

1 Like

Thanks Lemon_Juice! Great answer.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.