Tuesday, April 20, 2010

Workshop 6 PHP Strings, Dates and Forms

Working with Strings

1. Up to now, we've only been working on the echo statement. The printf() statement does the same thing except it can accept arguments passed onto the function.

<?php 

// This outputs the integer as a decimal number instead of part of a string 
printf("This is my number : "%d", 60) ; 

?> 


2. You can also specify the start position of your string displayed

<?php 

echo "<pre">; 
// This will leave 15 spaces before printing 
printf ("%15s\n", " Name"); 
printf ("%15s\n", " Address");
printf ("%15s\n", " Mobile Number");
printf ("%15s\n", " Email");
echo "<pre">;
?>

3. You can look up more string functions on www.php.net
• strlen() , to determine the length of a string
• strstr (), to determine the existence of a string within a string
• strpos(), to find the position of a substring


Working with Time and Date

1. PHP stores time as number of seconds that have elapsed since GMT midnight 1 January 1970. This is the time stamp

<?php 

// This is the timestamp, It is the number of seconds since Unix Epoch (Midnight GMT 1 Jan 1970) 

echo "The number of seconds that have passed since Midninght GMT 1 January 1970 is "; 
echo time(); 
echo "<br>"; 

?> 

2. With the timestamp you can convert it to date using getdate(). This is a an array that stores the vales of date.

<?php
$date_array = getdate(); // no argument passed so today's date will be used
foreach ($date_array as $key => $val) {
    echo "$key = $val<br>";
}
?>
<hr>
<?
echo "Today's date: ".$date_array['mday']."/".$date_array['mon']."/".
    $date_array['year']."<p>";
?>

Creating Forms

1. Use the following for testing.
1.1 Listing 9.1.html
<html>
<head>
<title>Listing 9.1 A simple HTML form</title>
</head>
<body>
<form action="listing9.2.php" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user">
<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea>
<P><input type="submit" value="send"></p>
</form>
</body>
</html>

1.2 Listing 9.2.php
<html>
<head>
<title>Listing 9.2 Reading input from a form </title>
</head>
<body>
<?php
echo "<p>Welcome <b>$_POST[user]</b></p>";
echo "<p>Your address is:<br><b>$_POST[address]</b></p>";
?>
</body>
</html>

There are two input boxes available from listing9.2.html called text and address. When the user submits the form it will invoke the script called listing9.2.php. The $_POST command will grab the value values corresponding to the text boxes value in listing9.1.html.


2. Getting data from a multi valued text box
2.1 Listing9.3.html
<html>
<head>
<title>Listing 9.3 An HTML form including a SELECT element</title>
</head>
<body>
<form action="listing9.4.php" method="POST">
<p><strong>Name:</strong><br>
<input type="text" name="user">

<p><strong>Address:</strong><br>
<textarea name="address" rows="5" cols="40"></textarea>

<p><strong>Select Some Products:</strong> <br>
<select name="products[]" multiple>
<option value="Sonic Screwdriver">Sonic Screwdriver</option>
<option value="Tricoder">Tricorder</option>
<option value="ORAC AI">ORAC AI</option>
<option value="HAL 2000">HAL 2000</option>
</select>

<p><input type="submit" value="send"></p>
</form>
</body>
</html>

2.2 Listing9.4.php
<html>
<head>
<title>Listing 9.4 Reading input from the form in Listing 9.3</title>
</head>
<body>
<?php
echo "<p>Welcome <b>$_POST[user]</b></p>";
echo "<p>Your address is:<br><b>$_POST[address]</b></p>";
echo "<p>Your product choices are:<br>";
if (!empty($_POST[products])) {
    echo "<ul>";
    foreach ($_POST[products] as $value) {
       echo "<li>$value";
    }
    echo "</ul>";
}
?>
</body>
</html>

3. Sending out emails

3.1 Listing9.10.html
<HTML>
<HEAD>
<TITLE>E-Mail Form</TITLE>
</HEAD>
<BODY>
<FORM action="listing9.11.php" method="POST">
<p><strong>Your Name:</strong><br> <INPUT type="text" size="25" name="name"></p>
<p><strong>Your E-Mail Address:</strong><br> <INPUT type="text" size="25" name="email"></p>
<p><strong>Message:</strong><br>
<textarea name="message" cols=30 rows=5></textarea></p>
<p><INPUT type="submit" value="send"></p>
</FORM>
</BODY>
</HTML>

3.2 Listing.9.11.php
<html>
<head>
<title>Listing 9.11 Sending mail from the form in Listing 9.10</title>
</head>
<body>
<?php
echo "<p>Thank you, <b>$_POST[name]</b>, for your message!</p>";
echo "<p>Your e-mail address is: <b>$_POST[email]</b></p>";
echo "<p>Your message was:<br>";
echo "$_POST[message] </p>";

//start building the mail string
$msg = "Name:     $_POST[name]\n";
$msg .= "E-Mail:   $_POST[email]\n";
$msg .= "Message:  $_POST[message]\n";

//set up the mail
$recipient = "youremail@youraddress"; //Remember to change the email to your address 
$subject = "ITC594 Workshop Testing";
$mailheaders = "From: My Web Site <http://192.168.6.200 \n";
$mailheaders .= "Reply-To: $_POST[email]";

//send the mail
mail($recipient, $subject, $msg, $mailheaders);
?>
</body>
</html>


0 comments:

Post a Comment