Write a PHP script to send form data

In this tutorial you’ll learn how to write a PHP script to send form data.

Write a PHP script to send form data

This is a follow up of the tutorials how to create a Bootstrap form and creating a MySQL database. To run a PHP script you need a web server that supports PHP and MySQL. If you haven’t installed this yet you can find the instruction how to do this in the tutorial explaining how to install PHP, MySQL and an Apache web server.

Link form to script

Before we start writing the PHP script we need to link the Form from our earlier tutorial and the file that will contain our script.

Open the file with the form and add the action and method attributes to the opening form tag:

Use the name you want to use for the file (with the PHP script) as the value for the action attribute (e.g. “send_formdata_members.php”). We’ll get to that shortly. Before that I want to briefly explain about the POST method.

Post vs Get

The $_GET and $_POST variables can both be used to collect data from a form.

With the GET method the information collected is visible to everyone (because it gets included in the URL). And the amount of information that you can send is limited.

With the POST method the information is not visible and there is no limit to the amount of information that can be send.

Because our form contains sensitive data we use the POST method.

PHP script

Create a new file with a PHP extension and use the file name you just inserted after the action attribute in the form. Insert the script below into this new php file:

PHP is a server side language. This means that the script is run on a web server and not in the browser.

You don’t need special software to write PHP scripts. You can do it with WordPad. But most of you will probably write HTML with a special editor. You can write the PHP script with that editor as well.

A PHP script starts with <?php and ends with ?>. This tells the server to parse the information between those tags as PHP.

Between these php tags you can see the variables (php variables start with a $) and statements we’ll use to insert the data into the database. I’ll walk you through them shortly.

Variables in PHP are case-sensitive. User-defined functions, classes, and keywords such as if, else, while, echo, and many more are case-insensitive.

You need to end each PHP statement with a semicolon.

The script above contains (single line) comments. These start with //. A comment in a PHP script on more than one line starts with /* and ends with */.

To output a message to the browser we can use the PHP echo ‘function’. As you can see above you can use HTML tags to format how the message should be rendered by the browser.

Your file with the script should not contain any other (HTML) code before or after this PHP script.

Connect to database on localhost

The script starts by logging in to your localhost (the web server on your local machine). We do this by creating a variable $link that holds the mysqli_connect function. If the script fails to log in we will get the error message “cannot connect to server”).

Replace password in the script with the password you chose for the ‘root’ user when you installed PHP, MySQL and Apache.

After logging in the script selects the the database “all_sports” with the mysqli_select_db function.

Submit input from form

The script then submits the data from the form into the database.

Before inserting the data the script first checks if the submit button in the form has been clicked with the $_REQUEST global variable and the ISSET function.

The submission of the data is then done in two steps (leaving the validation aside which we’ll look into in another post):

  1. Load data from form into variables

    We then see a list with variables (e.g. $firstname, $lastname, etc.). Each of these variables contains a value from an input field in the form. Because we used the POST method in the form these inputs are named $_POST[‘name’]. The names you see are those used with the name attribute (see the  input tags in the form).

  2. Insert values into database

    The second step is to use a query that inserts the values for the variables into the appropriate fields of the database/table.

    We create a variable “$insqDbTb” with the query to insert (into database and table) with the fields (firstname, lastname, etc.) the values currently contained in the variables ($firstname, $lastname, etc.).

    We then use the mysqli_query function to perform the query we just defined against the database. If there is an error we instruct the query to die (= stop).

Use form and script

To use the form and run the script you need to:

  1. Place the form and script file into the working directory for your local web server (e.g. “C:/Bitnami/wampstack-version.no/apache2/htdocs/”).
  2. Start the MySQL Database and the Apache web server.
  3. Open your browser and type in the address field: “localhost/name_formfile.html”.
  4. Insert data into the input fields of the form and click the button “Submit”.
  5. Check if the data were entered correctly in the database (log into phpMyAdmin and click on the name of the table. You should then see a record with the data you just entered in the form.

The next tutorial about client-side validation explains how you can add validation (states) to the form we developed here. After that we’ll examine how to add server-side validation to the PHP script.


Michael Maardt - April 25, 2016 Reply

Thanks for excellent tutorial. You write:
“Replace password in the script with the password you chose for the ‘root’ user when you installed PHP, MySQL and Apache.”
I use a normal webhost, therefore I did NOT create any pw for root, what do I do? I do have a db user with a known password. How can I use that? What do I type where?

Michael Maardt - April 25, 2016 Reply

I made it work ! Now: how do I send the user to a webpage saying “thank you. You data has been received” ?

Theo - April 26, 2016 Reply

Hi Michael,

you create a new HTML page with your “thank you” message and add an echo line to the PHP script redirecting to this new HTML page. See how I did this in my post http://tutsme-webdesign.info/bootstrap-3-contact-modal/ (specifically line 66 of the PHP script in that post).

Cheers, Theo

Theo - April 26, 2016 Reply

Hi Michael.


Prasanna - April 3, 2016 Reply

theo thank you very much for the this huge effort ( suggestion – will u pls do a small explanation on how to build a simple invoice with fpdf )

Theo - April 4, 2016 Reply

Thanks Prasanna!

About your suggestion: when I googled for a tutorial/script for an invoice with fpdf I already found several so I will not write one).

Cheers, Theo

Naveen - February 23, 2016 Reply

I am using bootstrap form validation for adding dynamic fields. I am having issues in inserting the data from these dynamic fields to mysql table. Any example for it would be greatful

Daniell - August 17, 2015 Reply

After pressing SUBMIT button, error message appears
Cannot POST /bootstrap-3.3.5-dist/send_formdata_members.php

PHP file should be in the directory of bootstrap?

Theo - August 17, 2015 Reply

Hi Daniell,

(In the demo I did not specify a different location for the php file. So if you use the same markup then:) the php file should be in the same directory as the html file containing the form.

Fred Odiboh - February 9, 2015 Reply

Thank you for your wonderful script and detailed explanation. i have a challenge using it on bootstrap framework. I inputted the html form into an htm page i framed with bootstrap, when i click the submit button, the “send_formdata_members.php” fill will not send the data to mysql database rather it will display the entire codes in the “send_formdata_members.php” file. But when i use the code on normal html without bootstrap element, everything works fine as you explained here. how can i make this work with bootstrap frame?

Theo - February 9, 2015 Reply

Hello Fred,

you wrote that the form doesn’t activate the PHP script correctly when inserted into a Bootstrap template. Did you use the Bootstrap template as explained in my previous post “http://tutsme-webdesign.info/bootstrap-form-with-a-script-sending-data-to-a-database/”. Then everything should work fine and the error might be caused by a simple typo. I suggest that you try the tutorials first with the exact code provided in my tutorial and then make the changes that you need. That way you can check step by step what causes the error.



Add your comment