1 / 15

CSC 3084: Web Development and Programming

CSC 3084: Web Development and Programming. Chapter 7: How to Work with Form Data. Input Text Boxes. Text input: The HTML for three types of fields <input type="text" name=" user_name " value=" rharris "/> <input type="password" name="password"/>

mira-long
Télécharger la présentation

CSC 3084: Web Development and Programming

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. CSC 3084:Web Development and Programming Chapter 7: How to Work with Form Data

  2. Input Text Boxes • Text input: The HTML for three types of fields <input type="text" name="user_name" value="rharris"/> <input type="password" name="password"/> <input type="hidden" name="action" value="login"/> <?php $user_name = $_GET['user_name']; $password = $_GET['password']; $action = $_GET['action']; ?>

  3. Radio Buttons <input type="radio" name="card_type" value="visa" checked="checked"/> Visa<br /> <input type="radio" name="card_type" value="mastercard"/> MasterCard<br /> <input type="radio" name="card_type" value="discover"/> Discover <?php if (isset($_POST['card_type'])) { $card_type = $_POST['card_type']; } else { $card_type = "unknown"; } ?>

  4. Check Boxes <input type="checkbox" name="pep" checked="checked"/> Pepperoni<br /> <input type="checkbox" name="msh"/> Mushrooms<br /> <input type="checkbox" name="olv"/> Olives <?php $pepperoni = isset($_POST['pep']); $mushrooms = isset($_POST['msh']); $olives = isset($_POST['olv']); ?>

  5. Check Boxes in an Array <input type="checkbox" name="top[]" value="pep"/> Pepperoni<br /> <input type="checkbox" name="top[]" value="msh"/> Mushrooms<br /> <input type="checkbox" name="top[]" value="olv"/> Olives $toppings = $_POST['top']; // get the toppings array $top1 = $toppings[0]; // $top1 is pep $top2 = $toppings[1]; // $top2 is olv $top3 = $toppings[2]; // $top3 is not set

  6. Check Boxes in an Array <?php if (isset($_POST['top'])) { $toppings = $_POST['top']; foreach($toppings as $key => $value) { echo $key. ' = ' . $value . '<br />'; } } else { echo 'No toppings selected.'; } ?>

  7. Drop-down List <select name="card_type"> <option value="visa">Visa</option> <option value="mastercard">MasterCard</option> <option value="discover">Discover</option> </select> <?php $card_type = $_POST['card_type']; ?>

  8. List Box w/o Multiple Selection <select name="card_type" size="3"> <option value="visa">Visa</option> <option value="mastercard">MasterCard</option> <option value="discover">Discover</option> </select>

  9. List Box w/ Multiple Selection <select name="top[]" size="3" multiple="multiple"> <option value="pep" selected="selected">Pepperoni</option> <option value="msh">Mushrooms</option> <option value="olv">Olives</option> </select>

  10. List Box w/ Multiple Selection <?php if (isset($_POST['top']) { $toppings = $_POST['top']; foreach ($toppings as $key => $value) { echo $key. ' = ' . $value . '<br />'; } } else { echo 'No toppings selected.'; } ?>

  11. Text Area <textarea name="comment" rows="4" cols="50"> Welcome to PHP and MySQL!</textarea> • The URL when using the GET method (spaces sent as plusses): process_data.php?comment=Welcome+to+PHP+and+MySQL! • Codes that are included when the user presses the Enter key: process_data.php?comment=Welcome+to%0D%0APHP+and+MySQL! • The PHP to get the data from the text area: <?php $comment = $_POST['comment']; ?>

  12. The htmlspecialcharsFunction htmlspecialchars($string[, $quote_style[, $charset [, $double_encode]]]) • Common HTML character entities: • Character Character entity & &amp; < &lt; > &gt; " &quot; ' &#039; Non-breaking space &nbsp;

  13. Character Entities Example • The text entered by the user: <?php $comment = $_POST['comment']; $comment = htmlspecialchars($comment, ENT_COMPAT, 'ISO-8859-1', false); ?> <p><?php echo $comment; ?></p>

  14. The nl2br Function • The nl2br function converts new line characters in a string to HTML <br> tags • The text entered into a text area: <?php $comment = $_POST['comment']; $comment = nl2br($comment, false); ?> <p><?phpecho $comment; ?></p>

  15. The echo and print Statements • The echo and print statements send strings to a web page • echo can accept one or more string values separated by commas, but print can only accept one (thus requiring concatenation) • The most important difference is that print returns a value and echo does not • This means that print statements can be written in expressions: <?php ($age >= 18) ? print('Can vote.') : print('Cannot vote.'); ?>

More Related