foundations to get you started
play

Foundations to Get You Started Beth Tucker Long Who am I? - PowerPoint PPT Presentation

Foundations to Get You Started Beth Tucker Long Who am I? Elizabeth Tucker Long (aka Beth) Editor-in-Chief of php|architect magazine Want to write? See me after. PHP Essentials Instructor Freelance consultant How this talk will


  1. Foundations to Get You Started Beth Tucker Long

  2. Who am I? • Elizabeth Tucker Long (aka Beth) • Editor-in-Chief of php|architect magazine Want to write? See me after. • PHP Essentials Instructor • Freelance consultant

  3. How this talk will work: • I'll be presenting the concepts on the left of the slide and the code on the right • Code will build from slide to slide, but due to space constraints, we can't show it all on one slide • The code being shown is for teaching use only, it is not optimized in any way. It should not be used as is for a live system.

  4. Getting Started <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 • Start with your Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1- standard HTML transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> • Opening PHP tag <head><title>Order Some Pizza</title</head> <body> • PHP code <h1>Welcome to Joe's Pizza!</h1> • Closing PHP tag <?php • Close out your HTML $name = "Beth"; page echo "<p>$name's order:</p>"; ?> </body></html>

  5. The Order Form <form action="./orderPizza.php" method="POST"> • We'll create a form <p>Name: <input type="text" name="custName" maxlength="200"/></p> that looks like this: <p>Choose a Size:<br /> <input type="radio" name="pizzaSize" value="Small" /> Small<br /> <input type="radio" name="pizzaSize" value="Medium" /> Medium<br Name: /> <input type="radio" name="pizzaSize" value="Large" /> Large</p> Choose a Size: <p>Add Additional Toppings:<br /> o Small <input type="checkbox" name="Mushrooms" value="Yes" /> Mushrooms<br /> o Medium <input type="checkbox" name="GreenPeppers" value="Yes" /> Green Peppers<br /> o Large <input type="checkbox" name="BlackOlives" value="Yes" /> Black Olives <br /> Add Toppings: <input type="checkbox" name="ExtraCheese" value="Yes" /> Extra Cheese<br /> Mushrooms Extra Cheese <input type="checkbox" name="Pepperoni" value="Yes" /> Pepperoni<br /> Green Peppers Pepperoni <input type="checkbox" name="Sausage" value="Yes" /> Sausage</p> Black Olives Sausage <input type="submit" name="pizzaStatus" value="Place Order" /> Place Order </form>

  6. What we get • Data from the form So the variables from our comes through as a form are: superglobal, • $_POST['custName'] automatically created • $_POST['pizzaSize'] by PHP for us in an array • $_POST['Mushrooms'] (a variable that is a container for other • $_POST['ExtraCheese'] variables): • $_POST['GreenPeppers'] • $_POST['Pepperoni'] $_POST['fieldName'] • $_POST['Black Olives'] • $_POST['Sausage'] • $_POST['pizzaStatus'] Signifies Array Array Variable Name Item

  7. Revised: The Order Form <form action="./orderPizza.php" method="POST"> • We'll create a form <p>Name: <input type="text" name="custName" maxlength="200" that looks like this: /></p> <p>Choose a Size:<br /> <input type="radio" name="pizzaSize" value="Small" /> Small<br /> Name: <input type="radio" name="pizzaSize" value="Medium" /> Medium<br /> Choose a Size: <input type="radio" name="pizzaSize" value="Large" /> Large</p> o Small <p>Add Additional Toppings:<br /> o Medium <input type="checkbox" name="pizzaToppings[]" value="Mushrooms" /> Mushrooms<br /> o Large <input type="checkbox" name="pizzaToppings[]" value="Green Peppers" /> Green Peppers<br /> <input type="checkbox" name="pizzaToppings[]" value="Black Add Toppings: Olives" /> Black Olives<br /> Mushrooms Extra Cheese <input type="checkbox" name="pizzaToppings[]" value="Extra Cheese" /> Extra Cheese<br /> Green Peppers Pepperoni <input type="checkbox" name="pizzaToppings[]" value="Pepperoni" Black Olives Sausage /> Pepperoni<br /> <input type="checkbox" name="pizzaToppings[]" value="Sausage" /> Sausage</p> <input type="submit" name="pizzaStatus" value="Place Order" /> Place Order </form>

  8. Revised: What we get • A superglobal that is also So the variables from our form are: an array is called a nested • $_POST['custName'] array (an array within an • $_POST['pizzaSize'] array): • $_POST['pizzaToppings'] $_POST['fieldName']['fieldname'] • $_POST['pizzaStatus'] Post Array custName pizzaToppings pizzaSize Beth Array Extra Large 0 1 2 Mushrooms Black Olives Extra Cheese

  9. Processing the Pizza Specs • We want to echo "<p>This order is for sort through {$_POST['custName']}</p> the toppings <p>Size: ".$_POST['pizzaSize']."</p> <p>Toppings:</p><ul>"; submitted. foreach($_POST['pizzaToppings'] as We'll use a $topping) { loop. echo "<li>$topping</li>"; } echo "</ul>";

  10. Making Decisions <?php if ($_POST['pizzaStatus'] == "Place Order") { echo "<p>This order is for {$_POST['custName']}</p> <p>Size: ".$_POST['pizzaSize']."</p> <p>Toppings:</p><ul>"; foreach($_POST['pizzaToppings'] as $topping) { echo "<li>$topping</li>"; } echo "</ul>"; } else { ?> <form action="./orderPizza.php" method="POST"> <p>Name: <input type="text" name="custName" maxlength="200" /></p> <p>Choose a Size:<br /> <input type="radio" name="pizzaSize" value="Small" /> Small<br /> <input type="radio" name="pizzaSize" value="Medium" /> Medium<br /> <input type="radio" name="pizzaSize" value="Large" /> Large</p> <p>Add Additional Toppings:<br /> <input type="checkbox" name="pizzaToppings[]" value="Mushrooms" /> Mushrooms<br /> <input type="checkbox" name="pizzaToppings[]" value="Green Peppers" /> Green Peppers<br /> <input type="checkbox" name="pizzaToppings[]" value="Black Olives" /> Black Olives<br /> <input type="checkbox" name="pizzaToppings[]" value="Extra Cheese" /> Extra Cheese<br /> <input type="checkbox" name="pizzaToppings[]" value="Pepperoni" /> Pepperoni<br /> <input type="checkbox" name="pizzaToppings[]" value="Sausage" /> Sausage</p> <input type="submit" name="pizzaStatus" value="Place Order" /> </form> <?php } ?>

  11. Validation $_POST['custName'] = htmlentities($_POST['custName']); Besides incomplete if (strlen($_POST['custName']) < 1) { submissions, $errorMessages[] = "Please enter your Name."; } we also always want if(!ctype_alpha($_POST['pizzaSize'])) { $errorMessages[] = "Please choose a Size."; to avoid } malicious submissions. if (is_array($_POST['pizzaToppings'])) { $checkToppings = implode("a",$_POST['pizzaToppings']); $checkToppings = str_replace(" ","a", $checkToppings); if(!ctype_alpha($checkToppings)) { $errorMessages[] = "Please choose some Toppings."; } } if(!ctype_alpha(str_replace(" ","a", implode("a",$_POST['pizzaToppings'])))) {

  12. Failure Happens When a validation if ($_POST['pizzaStatus'] == "Place Order") { test fails, make it easy //All our validation tests here for your user to fix it if(is_array($errorMessages)) { (Check for malicious echo "<ul>"; foreach($errorMessages as $message) submissions, but { always treat your echo "<li>$message</li>"; } users as though it echo "</ul>"; were an accident). //Form Code Goes Here } else { //Confirmation Code Goes Here } } else { //Form Code Goes Here }

  13. Reducing Redundancy Create the function: function displayForm($custName, $pizzaSize, $pizzaToppings) { //Echo Form code Here } And then use it whenever you need it: else { displayForm($_POST['custName'], $_POST['pizzaSize'], $_POST['pizzaToppings']); }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend