Save sqlinsertwithcode.php as sqlinsertwithformprocess.php. Open forms.html and save it as sqlinsertwithform.html (if you do not have that you will need to create a form which can submit two text fields). That page has two fields but they are the wrong ones so:
- change the name and id of the first one to personalName (remember to change the for attribute in the label element as well)
- do the same to change the second one to familyName
- change the type attribute of the second one to text
- change the label text to suit what you want the user to type in
- change the action to sqlinsertwithformprocess.php (first delete everything in those quotes first including the http)
The form should now ask the user for two names and then send that data to your processing page using POST. You cannot test this until you have changed the processing page.
On the processing page (sqlinsertwithformprocess.php) you need to change some things which might take a while to understand but are basically:
- take the data from the form
- put it into the database
More precisely this is what you should now do to sqlinsertwithformprocess.php:
- $_POST contains two pieces of data from the form:
- put each one into a variable ($personalName and $familyName) in the same sort of way you did in phpformprocess.php
- you already have a query but that currently inserts some text - we want to insert the values in the variables you just created:
- in the SQL query line delete Fred and Bloggs from the query but leave the single quotes that were around them (see note 1 below)
- in their place put the $personalName and $familyName variables (note 2 below)
Save both pages and try entering some names into the form and submitting. If you get a blank page it probably worked. You will check that next. This combination of form and processing is the foundation of almost all data entry on the Web. Think of any time you have typed something into a Web page and this was probably happening behind the scenes.
- You will probably now be getting a bit confused because we have at least five pairs of things using the same names. Each one is different so you could use different names but each one is involved with the same item of data so it makes sense to use the same name. Look at your code and make sure you know that:
- in the form each field has a matching id and a name (they do not need to match it is just easier that way)
- the labels have a for attribute which must match the id of the form field so that the label is attached to the correct field
- in the processing page the global variable $_POST has two indexes which were created by the name of each field in the form ($_POST["personalName"] and $_POST["familyName"])
- you then put the data from $_POST into two variables using the same names (again no need to use the same but it makes it easier to follow)
- in the query there are two database fields which have the same name as the variables containing the data which needs to go into them
- there is a reason Web developers are often well paid!