forms

Form processing with PHP: part 4

Form Validation

Validating a form

You can perform form validation before a form is submitted via JavaScript or after the form is submitted via PHP.

I've actually already demonstrated simple validation using PHP in Form Processing with PHP: Part 1, with the input.php script. After the form is submitted, the script checks to see whether a variable does or does not exist.

PHP Code:

<?php
//check if submit button clicked and the input field isn't empty, print hello
if ($submit == "click" && $UserName !=""):
echo "Hello, $UserName";

Form processing with PHP: part 3

Form Processing with PHP: Part 3

How to populate form fields with previously submitted values.

When you see the scripts in action, take a look at the source code, especially what the values of the form fields are set to before and after the scripts execute.

Input field:

To automatically have the value of an input field filled out, use the echo/print function to place the value submitted into the value attribute of the field.

PHP Code:

<?php
if ($submit && $firstname){
$fname = $firstname;
?>
 
<form method="post" action="populate1.php3">
What is your first name?

Form processing with PHP: part 2

Form Processing with PHP Part 2

Radio buttons, checkboxes, select lists

Radio Buttons:

<input type="radio" name="radioset" value="data" CHECKED> data

VALUE: Sets the value of the form element.
RADIOSET links a group of radio buttons together, only one radio per set can be checked.
RADIOSET is alsothe name of the variable that will contain the value submitted by the form.
CHECKED is an optional attribute that makes a radio button active by default.
If the value is not set, "on" is the default value submitted. You can't tell which button in the set was chosen.

 

Form Processing with PHP: Part 1

Form Processing with PHP: Part 1

Form Processing

Forms are usually used to gather information from visitors to your website. Once you've gathered the information, it can be used in several ways. It can be sent via email for someone to read, saved to database, or used to find data in the database that matches a specific criteria.

For example:

Populating checkboxes and select lists from an array

Populating checkboxes and select lists from an array

When you use checkboxes in a form, usually you want to save more than one choice.
You must indicate to PHP that you are submitting more than one piece of data with []

<INPUT TYPE="checkbox" NAME="temperature[]" VALUE="hot">Hot
<INPUT TYPE="checkbox" NAME="temperature[]" VALUE="cold">Cold

After submitting your form, save the array data into database.
You can use the implode function joins array elements into a string, then save that string to the
database.

PHP Code
 

Syndicate content