<?php include('includes/title.inc.php');  ?>
<?php
if(array_key_exists('send',$_POST)&&!$emaliSent)
{
$to = '1suming1@gmail.com,1suming@sina.com'; // use your own email address
$subject = 'Feedback from Japan Journey site';

// process the $_POST variables
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];

// build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";

$message .= "Comments: $comments";

$header='Dear webmaster';
$mailSent=mail($to, $subject, $message,$header);

}
?>


 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Japan Journey<?php if (isset($title)) {echo "&#8212;{$title}";} ?></title>
<link href="assets/journey.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="header">
<h1>Japan Journey </h1>
</div>
<div id="wrapper">
<?php include('includes/menu.inc.php'); ?>
<div id="maincontent">
<?php
if ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message.
Please try later.</p>
<?php
}
elseif ($_POST && $mailSent)
{
?>
<p><strong>Your message has been sent. Thank you for your feedback.
</strong></p>
<?php } ?>
<p>Ut enim ad minim veniam . . .</p>
<h1>Contact us  </h1>
<p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
<form id="feedback" method="post" action="">
<p>
<label for="name">Name:</label>
<input name="name" id="name" type="text" class="formbox" />
</p>
<p>
<label for="email">Email:</label>
<input name="email" id="email" type="text" class="formbox" />
</p>
<p>
<label for="comments">Comments:</label>
<textarea name="comments" id="comments" cols="60" rows="8"></textarea>
</p>
<p>
<input name="send" id="send" type="submit" value="Send message" />
</p>
</form>
</div>
<?php include('includes/footer.inc.php'); ?>
</div>
</body>
</html>


 

一段更好的代码,能够提示哪些必须要填的项目没填而且保存已填写的项目。(preserve the input )




<?php
include('includes/title.inc.php');
include('includes/corefuncs.php');
if (function_exists('nukeMagicQuotes')) {
nukeMagicQuotes();
}

// process the email
if (array_key_exists('send', $_POST)) {
$to = 'me@example.com'; // use your own email address
$subject = 'Feedback from Japan Journey site';

// list expected fields
$expected = array('name', 'email', 'comments');
// set required fields
$required = array('name', 'comments');
// create empty array for any missing fields
$missing = array();

// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}

// go ahead only if all required fields OK
if (empty($missing)) {
// build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments";

// limit line length to 70 characters
$message = wordwrap($message, 70);

// send it
$mailSent = mail($to, $subject, $message);
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
}
?>


 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Japan Journey<?php if (isset($title)) {echo "&#8212;{$title}";} ?></title>
<link href="assets/journey.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="header">
<h1>Japan Journey </h1>
</div>
<div id="wrapper">
<?php include('includes/menu.inc.php'); ?>
<div id="maincontent">
<h1>Contact us</h1>
<?php
if ($_POST && isset($missing)) {
?>
<p class="warning">Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message. Please try later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p><strong>Your message has been sent. Thank you for your feedback.</strong></p>
<?php } ?>
<p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
<form id="feedback" method="post" action="">
<p>
<label for="name">Name: <?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span class="warning">Please enter your name</span><?php } ?>
</label>
<input name="name" id="name" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['name']).'"';} ?>
/>
</p>
<p>
<label for="email">Email: <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Please enter your email address</span><?php } ?>
</label>
<input name="email" id="email" type="text" class="formbox"
<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email']).'"';} ?>
/>
</p>
<p>
<label for="comments">Comments: <?php
if (isset($missing) && in_array('comments', $missing)) { ?>
<span class="warning">Please enter your comments</span><?php } ?>
</label>
<textarea name="comments" id="comments" cols="60" rows="8"><?php
if (isset($missing)) {
echo htmlentities($_POST['comments']);
} ?></textarea>
</p>
<p>
<input name="send" id="send" type="submit" value="Send message" />
</p>
</form>
</div>
<?php include('includes/footer.inc.php'); ?>
</div>
</body>
</html>


If studying PHP code makes your brain hurt, you don’t need to worry about how this works. As long as you create the $expected, $required, and $missing arrays in  the previous step, you can just copy and paste the code for use in any form. So    what does it do? In simple terms,

 this foreach loop goes through the $_POST array,  strips out any whitespace from user input, and assigns its contents to a variable  with the same name (so $_POST['email'] becomes $email, and so on). If a

required field is left blank, its name attribute is added to the $missing array.

// go ahead only if all required fields OK

if (empty($missing)) {

// build the message

$message = "Name: $name\n\n";

$message .= "Email: $email\n\n";

$message .= "Comments: $comments";

// limit line length to 70 characters

$message = wordwrap($message, 70);

// send it

$mailSent = mail($to, $subject, $message);

if ($mailSent) {

// $missing is no longer needed if the email is sent, so unset it

unset($missing);

}

}

}

This ensures that the mail is sent only if nothing has been added to $missing.

However, $missing will be used to control the display in the main body of the

page, so you need to get rid of it if the mail is successfully sent. This is done by

using unset(), which destroys a variable and any value it contains.

Let’s turn now to the main body of the page. You need to display a warning if anything

is missing. Amend the conditional statement at the top of the page content

like this:



<h2>contact us</h2>
<?php if($_POST&&isset($missing)){ ?>
<p class="waring">Please complete the missing item(s) indicated.</p>
<?php } else if($_POST&&!mailSent) { ?>
<p class="warning">Sorry, there was a problem sending your message.
Please try later.</p>
<?php } elseif($_POST&&mailSent) {?>
<p><strong>Your message has been sent. Thank you for your feedback.
</strong></p>
<?php } ?>


 

To display a suitable message alongside each missing required field, add a PHP code

block to display a warning as a <span> inside the <label> tag like this:

<label for="name">Name: <?php

if (isset($missing) && in_array('name', $missing)) { ?>

<span class="warning">Please enter your name</span><?php } ?>

</label>

完整代码:





<?php
if (array_key_exists('send', $_POST))
{
$to="1suming@sina.com";
$subject="test";
$expected=array('name','email','comments');
$required=array('name','comments');
$missing=array();

foreach($_POST as $key=>$value)
{
$temp=is_array($value)?$value:trim($value);
if(empty($temp)&&in_array($key,$required))
{
array_push($missing,$key);
//$missing[]=$key;
}
elseif(in_array($key,$expected))
{
${$key}=$temp;
}
}


// go ahead only if all required fields OK
if (empty($missing)) {
// build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Comments: $comments";
// limit line length to 70 characters
$message = wordwrap($message, 70);
// send it
$mailSent = mail($to, $subject, $message);
if ($mailSent) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
}


 

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>


<style type="text/css">
body
{
width:750px;
margin:0 auto;
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
background-color:#CF9;
}

label
{
font-weight:bold;
color:#000;
}
.warning
{
font-weight:bold;
color:red;
}

</style>
</head>

<body>

<div id="header">
<h1>Japan Journey</h1>

</div>
<div id="content">
<h2>contact us</h2>
<?php if($_POST&&isset($missing)){ ?>
<p class="waring">Please complete the missing item(s) indicated.</p>
<?php } else if($_POST&&!$mailSent) { ?>
<p class="warning">Sorry, there was a problem sending your message.
Please try later.</p>
<?php } elseif($_POST&&$mailSent) {?>
<p><strong>Your message has been sent. Thank you for your feedback.
</strong></p>
<?php } ?>


<form id="feedback" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<p>
<label for="name">Name:</label>
<?php if(isset($missing)&&in_array('name',$missing)) { ?>
<span class="warning">Please enter your name</span> <?php } ?><br/>

<input name="name" id="name" type="text" class="formbox" <?php if(isset($missing)){
echo 'value="'.htmlentities($_POST['name']).'"' ; } ?>/>
</p>
<p>
<label for="email">Email:</label> <br/>
<input name="email" id="email" type="text" class="formbox" />
</p>
<p>
<label for="comments">Comments:</label>
<?php if(isset($missing)&&in_array('comments',$missing)) { ?>
<span class="warning">Please enter your comments</span> <?php } ?><br/>
<textarea name="comments" id="comments" cols="60" rows="8">
<?php if(isset($missing)){
echo htmlentities($_POST['comments']); } ?>
</textarea>
</p>
<p>
<input name="send" id="send" type="submit" value="Send message" />
</p>
</form>
</div>
<div id="footer">
</div>

</body>
</html>


单选按钮不同于text,

If no button  is selected, the radio button group’s $_POST array element remains unset. This

is different from the behavior of text input fields, which are always included in the

$_POST array, even if they contain nothing.

You need to take this into account in the code that preserves the selected value

when a required field is omitted. The following listing shows the subscribe radio

button group from contact.php, with all the PHP code highlighted in bold:



<fieldset id="subscribe">
<h2>Subscribe to newsletter?</h2>
<p>
<input name="subscribe" type="radio" value="Yes" id="subscribe-yes"
<?php
$OK = isset($_POST['subscribe']) ? true : false;
if ($OK && isset($missing) && $_POST['subscribe'] == 'Yes') { ?>
checked="checked"
<?php } ?>
/>
<label for="subscribe-yes">Yes</label>
<input name="subscribe" type="radio" value="No" id="subscribe-no"
<?php
if ($OK && isset($missing) && $_POST['subscribe'] == 'No') { ?>
checked="checked"
<?php } ?>
/>
<label for="subscribe-no">No</label>
</p>
</fieldset>


处理复选框同radio一样:



<fieldset id="interests">
<h2>Interests in Japan</h2>
<div>
<p>
<input type="checkbox" name="interests[]" value="Anime/manga" ➥
id="anime"
<?php
$OK = isset($_POST['interests']) ? true : false;
if ($OK && isset($missing) && in_array('Anime/manga', ➥
$_POST['interests'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="anime">Anime/manga</label>
</p>
<p>
<input type="checkbox" name="interests[]" value="Arts & crafts" ➥
id="art"
<?php
if ($OK && isset($missing) && in_array('Arts & crafts', ➥
$_POST['interests'])) { ?>
checked="checked"
<?php } ?>
/>
<label for="art">Arts & crafts</label>
</p>
. . .
</div>
</fieldset>


As with radio buttons, if no check box is selected, the $_POST['interests'] element

is not even created. So the code for the first check box contains the following:

$OK = isset($_POST['interests']) ? true : false;

 

Because the check box array might never be created, you need to set a default

value before attempting to build the body of the email. This time, rather than a

string, it needs to be presented as an array like this:

// set default values for variables that might not exist

$subscribe = isset($subscribe) ? $subscribe : 'Nothing selected';

$interests = isset($interests) ? $interests : array('None selected');

3. To extract the values of the check box array, you can use a foreach loop or the

implode() function. This oddly named function joins array elements. It takes two

arguments: a string to be used as a separator and the array. So, implode(', ',

$interests) joins the elements of $interests as a comma-separated string.

 

select box不同于上面,必有一个必须被选中。



<p>
<label for="select">How did you hear of Japan Journey?</label>
<select name="howhear" id="howhear">
<option value="No reply"
<?php
if (!$_POST || $_POST['howhear'] == 'No reply') { ?>
selected="selected"
<?php } ?>
>Select one</option>
<option value="foED"


<?php

if (isset($missing) && $_POST['howhear'] == 'foED') { ?>

selected="selected"

<?php } ?>

>friends of ED</option>

. . .

</select>

</p>


 

Multiple-choice lists are similar to check boxes: they allow the user to choose zero or more

items, so the result is stored in an array. If no items are selected, the $_POST array contains

no reference to the list, so you need to take that into consideration both in the form and

when processing the message.

1. The following code shows the first two items from the multiple choice list in contact.

php with the name attribute and PHP code highlighted in bold. Note that the

name attribute needs a pair of square brackets on the end to store the results as an

array. The code works in an identical way to the check boxes in PHP Solution 5-8.



<p>
<label for="select">What characteristics do you associate with ➥
Japan?</label>
<select name="characteristics[]" size="6" multiple="multiple" ➥
id="characteristics">
<option value="Dynamic"
<?php
$OK = isset($_POST['characteristics']) ? true : false;
if ($OK && isset($missing) && in_array('Dynamic', ➥
$_POST['characteristics'])) { ?>
selected="selected"
<?php } ?>
>Dynamic</option>
<option value="Honest"
<?php
if ($OK && isset($missing) && in_array('Honest', ➥
$_POST['characteristics'])) { ?>
selected="selected"
<?php } ?>
>Honest</option>
. . .
</select>
</p>