2-Chapter Edit-1
2-Chapter Edit-1
• Form used to gather input from users or used to pass data to a server.
• The <form> element defines a form that is used to collect user input:
The syntax:
<form action=“url to submit the form filled” method=“get” or “post”>
<!–- form contents -->
</form>
<Form> Attributes
name
method
action
enctype
name=“name” Value of attributes
method=“get, post”
enctype=“enctype” -specifies how the form-data should be encoded when submitting it to the
server {application/x-www-form-urlencoded, multipart/form-data, text/plain, … }
multipart/form-data – used when uploading files, does not encode any character. text/plain-
convert spaces into + symbols but special characters are not converted.
• Action – the URL of the script that the data will be sent to – this is the page that will display
once the submit button is clicked
• When a user click on the submit button, the form data is sent to a PHP file, called
"welcome.php":
• Method – Indicates how the information in the form will be sent to the web
• Can't be used to send binary data, like images or word documents, to the server because the
GET method sends the encoded user information.
• The data sent by GET can be accessed using QUERY_STRING environment variable.
• Never use GET method for systems which have password or other sensitive information.
• The information sent from a form with the GET method is visible to everyone (it will be
displayed in the browser's address bar). it is possible to bookmark the page.
• Relatively secured and could large data in requesting and responding data
• The POST method can be used to send ASCII as well as binary data.
• The data sent by POST method goes through HTTP header is secured enough on HTTP
protocol.
• Variables sent with HTTP POST are not visible in the URL. , it is not possible to bookmark
the page
• The Information sent from a form with the POST method is invisible to others
• The PHP header () function supplies raw HTTP headers to the browser and
can be used to redirect it to another location.
• The redirection script should be at the very top of the page to prevent any
other part of the page from loading.
• The target is specified by the Location: header as the argument to the header
() function. header("location:homepage.php").
• After calling this function the exit () function can be used to halt parsing of
rest of the code.
The $_REQUEST variable
PHP Form Validation
• User input should be validated whenever possible.
• For security reason, use server side validation if the form accesses a database.
• Server side form validation with PHP can act as a backup just in case the user
switch off java script support on the browser.
Cont..
• Form validation must be carried out on every form element to guarantee
that the input is correct and processing incorrect input values can make
your application give unpredictable result.
• A good way to validate a form on the server is to post the form to itself
<?php $_PHP_SELF ?>, instead of jumping (welcome.php) to a
different page. The user will then get the error messages on the same
page as the form.
The preg_match() function will tell you whether a string contains matches of a
pattern.
Cont..
• Presence Validation: check if there is something in a field or if a variable is not empty.
Cont..
• $password=”itec1234”;
• $min=6;
• $max=10;
• if(strlen($password)<$min&&strlen($password)>$max)
• Write a php program that can accept students age only 2 digits ?
Cont..
• Write a php program that can accept phone number which is started by +251 ?
Cont..
• Inclusion in set Validation: Is used to validate whether the value is in the set
• Write a php program that can accept students’ sex with letters m or f ?
Cont..
• Uniqueness Validation: Is used to validate whether the value which is going to be submitted to a
Cont..
• Format Validation: Is used to validate whether the value has the right format e.g.
email with @ symbol, currency with $ symbol, DateTime with AM or PM
• if(!preg_match("/^[@]+$/", $Email)){
• }else
•}
Cont..
• Validate e-mail address: Used to check an email is valid, i.e to have valid forms. if
(!filter_var($value, FILTER_VALIDATE_EMAIL))
• die("Invalid email format");
• Or
• if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$value))
• die("Invalid email format");
• URL Address: If there is an input field named "website" we can check for a valid
URL address like this
• if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-
9+&@#\/%=~_|]/i",$value)) {
• die("Invalid URL");
Refer the following PHP Built-in Functions
• Trim()
• Empty()
• Preg_match()
• Preg_replace()
• html_entities()
• Htmlspecialcharacters()
• Add_slashes()
• Strip_slashes()
• Mail()
Thank You!!!