Main Page

PHP code

Feedback: <textarea rows=”10” cols=”50” name=”feedbackText”>
</textarea><br />
<input type=”checkbox” name=”rememberMe” value=”yes” /> Remember Me<br />
<input type=”submit” value=”Submit Feedback” />
</form>
The server-side code (in this case, written in PHP) looks something like this:
<?php
//send e-mail
mail(“you@yourdomain.com”, “User Feedback”, $feedbackText,
“From: feedback@{$_SERVER[‘SERVER_NAME’]}”);
//if flag is set, set cookies
if ($rememberMe == “yes”) {
setcookie(“personName”, $personName, time() + 1000 * 60 * 60 * 24 * 365);
setcookie(“personEmail”, $personEmail, time() + 1000* 60 * 60 * 24 * 365);
}
?>
<!-- thank you message goes here -->
This PHP code first sends the feedback e-mail using PHP’s
mail()
function and then checks to see if the
user wants to be remembered, meaning that the value of
$rememberMe
equals
yes
. If so, cookies are
used to store the user ’s name and e-mail address. Both cookies are set to expire a year after the current
day by using the PHP
time()
function and adding the value of 365 days in milliseconds (1000 millisec-
onds x 60 seconds x 60 minutes x 24 hours x 365 days).
In PHP, the values of form fields are made accessible as variables with the same name as the form field,
so the value in the text box named personName is accessible as a string variable
$personName
.
Remember to include at least the
getCookie()
function on the feedback form page to retrieve the value
of each cookie. Then, in the
onload
event handler, look for a cookie containing user information and, if
you find it, assign that information back into the form.
window.onload = function () {
var sName = getCookie(“personName”);
var sEmail = getCookie(“personEmail”);
if (sName && sEmail) {
var oForm = document.forms[“feedbackForm”];
oForm.personName.value = sName;
oForm.personEmail.value = sEmail;
}
};
This JavaScript code checks to see if both the person’s name and e-mail address have been stored in cook-
ies. If they have, the values are assigned to the appropriate fields in the form. Now, your users won’t have
to type in the same contact information each time they send feedback (well, at least for a year).
489
Client-Server Communication
19_579088 ch16.qxd 3/28/05 11:42 AM Page 489


JavaScript EditorFree JavaScript Editor     Ajax Editor


©