150 likes | 245 Views
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"/>
E N D
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"/> <input type="hidden" name="action" value="login"/> <?php $user_name = $_GET['user_name']; $password = $_GET['password']; $action = $_GET['action']; ?>
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"; } ?>
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']); ?>
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
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.'; } ?>
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']; ?>
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>
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>
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.'; } ?>
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']; ?>
The htmlspecialcharsFunction htmlspecialchars($string[, $quote_style[, $charset [, $double_encode]]]) • Common HTML character entities: • Character Character entity & & < < > > " " ' ' Non-breaking space
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>
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>
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.'); ?>