1 / 35

PHP – Get & Post; Functions; and Arrays

PHP – Get & Post; Functions; and Arrays. IS6116 – 07 th February 2011. Forms and PHP. The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input Form elements in an HTML/PHP page will automatically be available to your PHP scripts. $_POST.

meli
Download Presentation

PHP – Get & Post; Functions; and Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP – Get & Post; Functions; and Arrays IS6116 – 07th February 2011

  2. Forms and PHP • The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input • Form elements in an HTML/PHP page will automatically be available to your PHP scripts.

  3. $_POST • The $_POST variable is an array of variable names and values sent by the HTTP POST method • The $_POST variable is used to collect values from a form with method="post“ • Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send

  4. Example - $_POST <html> <body> <form action="newpage.php" method="post"> Name: <input type="text" name="name"> Age: <input type="text" name="age"> <input type="submit"> </form> </body> </html>

  5. Example - $_POST continued <html> <head> <title>Grab form values</title> </head> <body> Welcome <?php echo $_POST["name"];?>.<br/> You are <?php echo $_POST["age"];?> years old </body> </html>

  6. $_GET • The $_GET variable is an array of variable names and values sent by the HTTP GET method • The $_GET variable is used to collect values from a form with method="get" • Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and it has limits on the amount of information to send (max. 100 characters)

  7. Example - $_GET <html> <body> <form action="get.php" method="get"> Name: <input type="text" name="name"> Age: <input type="text" name="age"> <input type="submit"> </form> </body> </html>

  8. Example - $_GET <html> <head> <title>Grab form values</title> </head> <body> Welcome <?php echo $_GET["name"];?>.<br/> You are <?php echo $_GET["age"];?> years old </body> </html>

  9. htmlentities • An inbuilt function that takes a string and returns the same string with HTML converted into HTML entities. • For example, the string "<script>" would be converted to "&lt;script&gt;". • Prevents the browser from using input values as HTML code!

  10. Multiform inputs <HTML> <HEAD> <TITLE>Multi-page Form - Page One</TITLE> </HEAD> <BODY> <p>Please fill in the following information</p> <FORM METHOD="POST" ACTION="page2.php"> Name: <INPUT TYPE="text" SIZE="40" name="cust_name"><BR> Email: <INPUT TYPE="text" SIZE="40" name="cust_email"><BR> <INPUT TYPE="submit" name="submit1" value="Proceed"> </FORM> </BODY> </HTML>

  11. page2.php <?php $cust_name = htmlentities ($_POST['cust_name']); $cust_email = htmlentities ($_POST['cust_email']); ?> <HTML> <HEAD> <TITLE>Multi-page Form - Page Two</TITLE> </HEAD> <BODY> <p>Please fill in the following information</p> <FORM METHOD="POST" ACTION="final.php"> Address: <INPUT TYPE="text" SIZE="50" name="cust_address"><br/> Phone: <INPUT TYPE="text" SIZE="20" name="cust_phone"><br/> <INPUT TYPE="hidden" name="cust_name" VALUE="<?php echo $cust_name; ?>"> <INPUT TYPE="hidden" name="cust_email" VALUE="<?php echo $cust_email; ?>"> <INPUT TYPE="submit" name="submit2" value="Proceed"> </FORM> </BODY> </HTML>

  12. final.php <?php $cust_name = htmlentities($_POST['cust_name']); $cust_email = htmlentities($_POST['cust_email']); $cust_address = htmlentities($_POST['cust_address']); $cust_phone = htmlentities($_POST['cust_phone']); ?> <HTML> <HEAD> <TITLE>Multi-page Form - Final</TITLE> </HEAD> <BODY> <p>You filled in:</p> Name: <?php echo $cust_name; ?><br/> Email: <?php echo $cust_email; ?><br/> Address: <?php echo $cust_address; ?><br/> Phone: <?php echo $cust_phone; ?><br/> </BODY> </HTML>

  13. Functions • 2 types: • Built in functions • Custom defined functions • Functions minimize the amount of repetition of code. • function consists of function name followed by parentheses. Some functions may need values passed to it.

  14. Built in Functions • Thousands of built in functions. Can be found on w3schools.com or php.net • Example built-in function: strtoupper() • strtoupper(“php on Mondays”); • converts string “php on Mondays” to “PHP ON MONDAYS”

  15. Custom Defined Functions • Syntax: • function name_of_function($arg1, $arg2) { //code of the function } Note: You can but are not required to include arguments within the parentheses!

  16. Simple function without arguments • function say_hello() { echo “<p>Hello everybody!</p>”; } • To call the function: say_hello();

  17. Function requiring an Argument • <?php function printBR($txt) { echo $txt.”<br/>”; } printBR(“Line one!”); printBR(“Line two!”); ?>

  18. Function to return a value (s) • <?php function addNums($first, $second) { $result = $first + $second; return $result; } echo addNums(10, 3); //Sends 10 and 3 as arguments to the addNums function and will print 13

  19. Variable Scope • Variables declared within functions remain local to those functions (i.e. they will not be accessible outside the function) • function $test(){ $testvariable = “this is a test variable”; } echo “test variable is “.$testvariable;

  20. function $test(){ $testvariable = “this is a test variable”; } echo “test variable is “.$testvariable; • Will create an error!

  21. Variable scope continued • Variables defined outside functions are inaccessible within functions by default • Following will output an error! $testvariable = “this is a test variable”; function $test(){ echo $testvariable; } test();

  22. Variable Scope Continued • Use global statement to access variable in a function that has been defined outside the said function $testvariable = “this is a test variable”; function $test(){ global $testvariable; echo $testvariable; } test();

  23. Passing by value & by reference • When passing arguments to functions, they are stored as copies • Any changes made to the variable within the body of a function are local to that function and do not extend to outside

  24. Passing by Value - Example • <?php function addFive($num){ $num += 5; } $originalNum = 10; addFive($orignalNum); echo $originalNum; //Outputs 10 ?>

  25. Passing by Reference - Example • <?php function addFive(&$num){ $num += 5; } $originalNum = 10; addFive($orignalNum); echo $originalNum; //Outputs 15 ?> Passing by Reference gives access to the actual variable that is passed rather than just a copy of the variable as per passing by Value

  26. Arrays • Variables typically store one value at a time (i.e. a variable may store a single colour such as red, blue, green, or some other colour • However, where you may need to store a list of values in a variable rather than just one value an array is suitable • E.g. a normal variable may store a value for the colour variable whereas an array facilitates storing a list of values for an array

  27. Creating arrays • number of ways to create arrays • array() function (typically used when populating the values of array in one go!) • the array operator [] (typically to store one value initially allowing for additional values later)

  28. Array Example • $colours = array (“red”, “blue”, “green”, “yellow”, “black”, “purple”); • Same as: $colours[] = “red”; $colours[] = “blue”; $colours[] = “green”; and so on....

  29. Associative Arrays • A variable array capable of storing various different elements and associated values • $person = array(“name” => “John”, “age” => 10, “occupation” => “PHP Coder”); • $person[‘age’] = 10; $person[‘name’ = “John”; • echo $person[‘age’];

  30. Multidimensional Arrays • Facilitates storing arrays of arrays! • Extends beyond the associative array to store multiple sets of associative arrays

  31. Multidimensional Example • <?php $person = array( array(“name” => “John”, “age” => 19), array(“name” => “Mary”, “age” => 30), array(“name” => “Aine”, “age” => 23) ); ?>

  32. Multidimensional Arrays • each of the array elements store starting at an index value of 0 • for instance: • echo $person[0]; • echo $person[1]; • and so on and on and on!

  33. Multidimensional Arrays • foreach ($person as $p) { while (list($n, $a) = each ($p)) { echo “Name: “.$n.”\n Age: ”.$a.”<br/>”; } } • Output: Name: John Age: 19 Name: Mary Age: 30 Name: Aine Age: 23

  34. In built functions suitable for Arrays • count() • sizeof() • each() • list() • foreach() • array_push() - Adds 1 or more elements to the end of an existing array • e.g. array_push($person, “Harry”, 24); • More array related functions – can be found at php.net/array or w3schools.com and so on

More Related