Processing HTML POST form with PHP

#1

I’m creating a website through wordpress where users choose their phone and which repairs they need using an HTML form and I’m trying to send that to an order confirmation page where depending on what information they entered in the form, a price will be shown.

-In the action parameter of the form, do I have to enter a PHP file or the URL of the order confirmation page?
-Where does my PHP file have to be located in my websites directory?
-How do I parse the forms information and can I save the information to be stored in variables for the prices?

0 Likes

#2

The action URL will parse the form data. There are many ways to do it, but you could for example use headers to forward the user to confirmation on success or back to the form in the case of an error.
That URL could call the parsing script, then display the appropriate page itself depending on the result.

The URL must be somewhere accessible (above the root), though the code it calls may be kept out on reach.

This can get quite involved, but the give the basics, first check for post submission.

if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
    // Process form
}

The form data will be in the $_POST array, eg: $_POST['phone'] with keys names after your form inputs.

Validate the data, as in make sure the values are the kind of data you are expecting. It may differ depending on user error or hack/spam attemts which needs to be handled accordingly.

It’s simple to assign an input value to a variable $phone = $_POST['phone']; though that in itself is a bit pointless.
Prices I guess would be stored in a database, so what you woild probably do with the input data is use it to make a selection from a price list table for the selected product/service.

Obviously there is much more detail needed to flesh this out, but the specifics should be dealt with a step at a time after you plan out exactly how you want this to work.

0 Likes

#3

Thanks for the reply!

From what I’m testing, I can echo the form data on a blank page, but how can I carry the data submitted to a page already created and display it? IE: The user completes a form saying they want a screen replacement and a home button replacement on example.com/repairs then this is shown in a shopping cart style format on example.com/order-confirmation

0 Likes

#4

All the input will be available in the $_POST array in the script called by the form’s action attribute.
That script may (depending on the result of the form) display either the form again to correct errors, or the order confirmation.
Though if you want the data to persist further (from page to page) as in a “shopping cart” it could be stored in the session.
So, this is just one example how it may be done, the action could keep you with the form page, so in case of errors it simply displays the form again for correction.
But on successful completeion of the form, relevant data (after processing) is stored into the session and you are forwarded to the order confirmation page.

0 Likes