Learning PHP – Forms, textboxes, and submit buttons

MySQL and PHP have been installed, the database schema has been designed, the database script has been started, and my web development tool, Microsoft Expressions, is ready to go. It’s time to start designing the interface and using PHP to gather input and insert it into the database.

I started at the beginning by designing the login page using HTML and CSS. It took some practice to learn how to use PHP to get values from the textboxes using a form. PHP is designed to be pretty simple to use and likes the input type of ‘submit’, which is different than the ‘button’ input which triggers an action that is assigned to it. Submit creates a button, but is used specially for PHP to submit the data from HTML input fields (such as textboxes, drop down lists, radio button, check boxes, etc.) to the server. Learning that PHP uses the name attribute of an input rather than the ID, which Javascript likes to use, hung me up for a bit. Here is an example:

<input type=’text’ name=’first_name’ id=’first_name’ />

To get the input from this with PHP, I use the isset built in method to determine that an input is set and not null. As I previously said, you must address the input by it’s name attribute or the $_POST variable will not be able to find it. I think it is good practice to also assign inputs an ID that is the same as the name in case we need to use Javascript to alter it.

if (isset($_POST[“first_name”)) {$firstname = $_POST[“first_name”];}

When creating a form, you must assign an action to it – either GET or POST. From what I’ve read, POST is the preferred method and is more secure. So when the submit button is pressed, PHP looks at all of the inputs on the page and assigns the value of the input to the built in variable of $_POST[“name_of_the_input“] which can then be transferred to a standard PHP variable such as $firstname. To test, we can assign an echo action to the submit button.

First, I’ll check to see if the submit button has been checked using the isset() method:

if (isset($_POST[“submit”]))

Then I’ll actually transfer the code to grab the variable for the first name and place it inside the submit action to be safe.

{ if (isset($_POST[“first_name”))

{$firstname = $_POST[“first_name”];}

echo $firstname; }

Assuming all of the names of the inputs are the same as what I’ve assigned, when pressing the submit button I should see the input in the first_name box display on the screen. This is a typical way to grab user’s input and will be used heavily throughout the project.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s