0% found this document useful (0 votes)
7 views

Ch2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Ch2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

University of Somalia (UNISO)

Faculty of Engineering & Computer Science

COURSE: PHP AND MYSQL


Chapter 2:
Programming with
PHPEng. Ismail Mohamed Jamal
MCSE (Master of Computer Engineering)
Ondokuz Mayis University - Turkiye (OMU)
Email: engismailmj@gmail.com

1
Objectives

 Creating an HTML Form

 Handling an HTML Form

 Conditionals and Operators

 Validating Form Data

 Introducing Arrays

 For and While Loops

2
Introduction

• In this chapter, you’ll begin to create more elaborate scripts


while still learning some of the standard constructs functions
and syntax of the language.

• We are going to create HTML Form, then learning how you


can use PHP to handle the submitted values.

• From here , the chapter covers conditionals and the


remaining operators, arrays and loops.

3
Creating an
HTML Form
• Handling an Html Form with PHP is perhaps the most important process
in any dynamic web sites.

• Two steps are involved:

1. You create the Html Form itself

2. You create the corresponding PHP scripts that will receive and the
process the Form data.

4
Continue

• An HTML Form is created using the form tags and various element for taking
input .

• The form tags look like:

<Form action=“script.php” method=“post”>

</form>

• In terms of PHP, the most important attribute of your form tag is action.
Which dictates to which page the form data will be sent.

• The second attribute is method , which has its own issues.

5
Choosing a
Method
• The method attribute of a Form dictates how the data is sent to
the handling page.

• Two options:- get and post : refer to the HTTP method to be


used.

• The get method sends the submitted data to the receiving page
as a series of name_value pairs appended to the URL.

• The benefit of using the get method is that the resulting page
can be bookmarked in the user’s Web browser (since it’s a
URL).
6
Continue

• The drawback of get method is less secure (Since the data is


visible).

• Generally speaking, get is used for requesting information


like a particular record from a database or the result of a
search.

• The post method is used when an action is required as when


a database record will be updated or an email should be
sent.
7
PHP - A Simple HTML
Form

<html>
<body>

<form action="welcome.php" method="post">


Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>
8
Continue

• When the user fills out the form above and clicks the submit button,
the form data is sent for processing to a PHP file named
"welcome.php". The form data is sent with the HTTP POST method.

Welcome <?php echo $_POST["name"]; ?><br>


Your email address is: <?php echo $_POST["email"]; ?>

9
GET vs. POST

When to use GET?

• Information sent from a form with the GET method is visible to


everyone (all variable names and values are displayed in the URL). GET
also has limits on the amount of information to send. The limitation is
about 2000 characters. However, because the variables are displayed in
the URL, it is possible to bookmark the page. This can be useful in some
cases.
• GET may be used for sending non-sensitive data.

• Note: GET should NEVER be used for sending passwords or other sensitive information!

10
Continue

When to use POST?

• Information sent from a form with the POST method is invisible to


others (all names/values are embedded within the body of the HTTP
request) and has no limits on the amount of information to send.

• Moreover POST supports advanced functionality such as support for


multi-part binary input while uploading files to server.

• However, because the variables are not displayed in the URL, it is not
possible to bookmark the page.

• Developers prefer POST for sending form data.

11
PHP Operators

Operators are used to perform operations on variables and values.


PHP divides the operators in the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Increment/Decrement operators
5. Logical operators
6. String operators (assignment)
7. Array operators
8. Conditional assignment operators (assignment)

12
PHP Assignment
Operators

• The PHP assignment operators are used with numeric values to write
a value to a variable.

• The basic assignment operator in PHP is "=". It means that the left
operand gets set to the value of the assignment expression on the
right.
<?php
<?php
$x = 20;
$x = 10;
$x += 100;
echo $x;
echo $x;
?>
?>
13
PHP Increments /
Decrement Operator
• The PHP increment operators are used to increment a variable's value.
• The PHP decrement operators are used to decrement a variable's value.

<?php
$x = 10;
echo ++$x;
?>

14
Comparative
Operators
Symbol Meaning Example

== Is equal to $x=$y

!= Not equal to $x !=$y

< Less than $x<$y

> Greater then $x>$y

<= Less than or equal to $x<=$y

>= Greater than or equal to $x>=$y

15
Logical Operators

Symbol Meaning Example

! not !$x

&& and $x &&$Y

|| Or $x ||$Y

XOR and not $x XOR$Y

16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy