introduction to php web programming - get and post

25
Introduction to PHP Web programming Week 11, day2

Upload: baabtracom-first-coding-school-in-india

Post on 22-May-2015

551 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Introduction to php  web programming - get and post

Introduction to PHPWeb programming

Week 11, day2

Page 2: Introduction to php  web programming - get and post

Web programming

Page 3: Introduction to php  web programming - get and post

PHP with HTML

• You can embed PHP code inside html:<html>

<body>

<h1>

<?php

$str_one = "Helloo World!";

echo $str_one;

?>

</h1>

</body>

</html>

• Or you can call html from php:<?php

echo "<html><body><h2>Helloo Baabtra</h2></body></html>”;

?>

Page 4: Introduction to php  web programming - get and post

Client - Server communication with Form Data

Page 5: Introduction to php  web programming - get and post

When you submit the form- all form element data(username and password ) will

be send to the file ‚registration.php‛ which is residing on your server

HTML Form Example

Page 6: Introduction to php  web programming - get and post

HTML Form Example

Action specifies where to send the form data? Normally we specify a page which resides on server so that all form data will be

sent to that page. In this example when you click submit button data you entered will be send to “registation.php”

Page 7: Introduction to php  web programming - get and post

HTML Form Example

We can send data to server in two ways GET or POST.

Page 8: Introduction to php  web programming - get and post

Form action and methods

•There are two methods that we can send form data from client to server

• GET

• Allows you to send data as part of the query string.That means data will

be appended with the url

•http://example.org/index.php?list=user&orderby=name&direction=asc

• POST

Allows the client to send along a data payload, for example, if you are

posting an HTTP form, the payload could consist of the form data, while if

you are uploading a file, the payload would consist of the file itself. Payload

will be with HTTP request so that it will not be visible to the user

Page 9: Introduction to php  web programming - get and post

Comparison

• Data sent will be shown in the URL

• Size of Data can be send is limiter

normally ~2kb but its browser

dependant

• Cannot be used to send binary

data like image or document

• Bookmarking is possible with GET

method

• Data will not be shown with the browser

• No restrictions on size of data

• Can be used to send files

• Book marking is not possible with POST

method

GET POST

Page 10: Introduction to php  web programming - get and post

How will you get that data in ‚registration.php‛ ???

• All the form data sent via Get method will be stored in a super global Array

$_GET

• Eg: http://example.org/index.php?list=user&orderby=name&direction=asc

echo $_GET*’list’+;

• You can create arrays by using array notation...

Eg: http://example.org/index.php?list=user&order[by]=column&order[dir]=asc

•and then access them using the following syntax:

echo $_GET*’order’+*’by’+;

echo $_GET*’order’+*’direction’+;

Page 11: Introduction to php  web programming - get and post

How will you get that data in ‚registration.php‛ ???

• Similarly all the form data sent via POST method will be stored in a super global

array $_POST

•Eg:

echo $_POST*‘name’+; // will output baabtra

echo $_POST*‘email’+; // will output [email protected]

Page 12: Introduction to php  web programming - get and post

When You Don’t Know How Data Is Sent

• If you want to receive data send by client browser but you don’t know the method

they used to send (GET or POST), you can do this by using $_REQUEST[] super

global array

• ie. $_REQUEST[] will contain what ever data you send in any method either GET

or POST.

• Eg: echo $_REQUEST*‘name’+;

Page 13: Introduction to php  web programming - get and post

Try this• Create a HTML form as below . Try both Get and POST methods

• Up on clicking submit button it should display what user have

entered

localhost/test/Registration.php

[email protected]

localhost/test/Registration.php

Page 14: Introduction to php  web programming - get and post

Managing File uploads

Page 15: Introduction to php  web programming - get and post

File Uploading

• File uploads are an important feature for many Web applications;

• A file can be uploaded through a ‚multi-part‛ HTTP POST

transaction.

<form enctype="multipart/form-data" action="index.php‚ method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="50000" />

<input type="file" name="filedata" />

<input type="submit" value="Send file" />

</form>

Page 16: Introduction to php  web programming - get and post

File Uploading

• File uploads are an important feature for many Web applications;

• A file can be uploaded through a ‚multi-part‛ HTTP POST

transaction.

<form enctype="multipart/form-data" action="index.php‚ method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="50000" />

<input type="file" name="filedata" />

<input type="submit" value="Send file" />

</form>

is an encoding type that allows files to be sent through a POST. Quite simply, without this

encoding the files cannot be sent through POST.

Page 17: Introduction to php  web programming - get and post

File Uploading

• File uploads are an important feature for many Web applications;

• A file can be uploaded through a ‚multi-part‛ HTTP POST

transaction.

<form enctype="multipart/form-data" action="index.php‚ method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="50000" />

<input type="file" name="filedata" />

<input type="submit" value="Send file" />

</form>

MAX_FILE_SIZE value is used to define the maximum file size allowed(in this case, 50,000 bytes)

Page 18: Introduction to php  web programming - get and post

File Uploading

• File uploads are an important feature for many Web applications;

• A file can be uploaded through a ‚multi-part‛ HTTP POST

transaction.

<form enctype="multipart/form-data" action="index.php‚ method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="50000" />

<input type="file" name="filedata" />

<input type="submit" value="Send file" />

</form>

Tip :You can limit the amount of data uploaded by a POST operation

by modifying number of configuration directives in php.ini, such as

post_max_size, max_input_time and upload_max_filesize.

Page 19: Introduction to php  web programming - get and post

How will you receive the file at server side?

• Once a file is uploaded to the server, PHP stores it in a temporary location and

makes it available to the script; It is up to the script to move the file to a safe

location. The temporary copy is automatically destroyed when the script ends.

• Inside your script, uploaded files will appear in the $_FILES super global multi

dimensional array. This array will have the following keys

– name : The original name of the file

– type : The MIME type of the file provided by the browser

– size : The size (in bytes) of the file

– tmp_name : The name of the file’s temporary location

– error : The error code associated with this file. A value of

UPLOAD_ERR_OK indicates a successful transfer

Page 20: Introduction to php  web programming - get and post

Built in functions for file upload()

• is_uploaded_file(filename) : returns true if the file has uploaded to the

temporary location in server

• Accepts and argument ‚filename‛ which refers to the name of the file in servers

temporary position.ie it will be $_FILE*‘filename’+*‘tmp_name’+

• move_uploaded_file(source,destination) : to move an uploaded file to a

different location

• Source refers to its current location which obviously is in the temporary

location in the server ie $_FILE*‘filename’+*‘tmp_name’+

• Destination refers to where to move the file along with its name eg.

‚images/profilePics/‛. $_FILE*‘filename’+*‘name’+

Page 21: Introduction to php  web programming - get and post

form enctype="multipart/form-data" action=‚register.php‚ method="post">

<input type="file" name="filedata" />

<input type="submit" value="Send file" />

</form>

Example – File Upload

Index.html

Page 22: Introduction to php  web programming - get and post

<?php

echo $_FILE*‘filedata’+*‘name’+; // prints the name of file uploaded

echo $_FILE*‘filedata’+*‘size’+; // prints the size of file uploaded

is_uploaded_file($_FILE*‘filedata’+*‘tmp_name’+)

{

$target=‚images/‛.$_FILE*‘filedata’+*‘name’+;

move_upload_file($_FILE*‘filedata’+*‘tmp_name’+,$target);

}

?>

Example – File Upload

Register.php

Page 23: Introduction to php  web programming - get and post

Questions?

‚A good question deserve a good grade…‛

Page 24: Introduction to php  web programming - get and post

End of day

Page 25: Introduction to php  web programming - get and post

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]