">
Csc 3084 web development and programming
This presentation is the property of its rightful owner.
Sponsored Links
1 / 15

CSC 3084: Web Development and Programming PowerPoint PPT Presentation


  • 42 Views
  • Uploaded on
  • Presentation posted in: General

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"/>

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Csc 3084 web development and programming

CSC 3084:Web Development and Programming

Chapter 7:

How to Work with Form Data


Input text boxes

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

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

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

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 array1

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

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

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

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 selection1

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

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 htmlspecialchars function

The htmlspecialcharsFunction

htmlspecialchars($string[, $quote_style[, $charset

[, $double_encode]]])

  • Common HTML character entities:

  • CharacterCharacter entity

    &&amp;

    <&lt;

    >&gt;

    "&quot;

    '&#039;

    Non-breaking space&nbsp;


Character entities example

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

  • 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

  • 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.');

    ?>


  • Login