Intro:Processing a member registration form

本文介绍了一个体育俱乐部会员申请表单的设计与实现过程。表单收集姓名、地址等信息,并通过PHP验证这些信息的有效性。如果信息有效,则通过邮件发送给管理员。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Processing a member registration form
Now that you've seen some of PHP's built-in functions,let's apply some of what you've learned to a practial application:a membership application form for a sports club.This form will ask the application to enter various bits of personal information;it will then validate this information and,if acceptable,will formulate and send an e-mail with the application to the club administrator.
Here's the HTML form(reg.html)
<?xml version="1.0" encoding="utf-8" ?>
<!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" xml:lang="en" lang="en">
<head>
   <title>Project:Member registration</title>
</head>
<body>
<h2>Project:Member registration<h2>
Name:<br />
<input type="text" name="name" size="50" />
<p />
Address:<br />
<textarea name="address" row="5" cols="40"></textarea>
<p />
Age:<br />
<input type="text" name="age" size="3" />
<p />
Porfession:<br />
<input type="text" name="profession" size="50" />
<p />
Residential status:<br />
<input type="radio" name="resident" value="yes" checked="true" />Resident
<input type="radio" name="resident" value="no" />Non-Resident
<p />
<input type="subimt" name="submit" value="subimt" />
</form>
</body>
</html>
----------------------------------------------------------------------------------
This form has five input fields,one each for the applicant's name,address,age,profession,and residential status.

NOTE
in order to successfully send mail with PHP using the mail() function,your php.ini configuration file must include some information about the mail server or mail agent to be used.Windows user will need to set the 'SMTP' and 'smtp_port' options,while *NIX users may need to set the 'sendmail_path' option.More information on these options can be obtained form the PHP manual page at www.php.net/mail.

Once the form is sumbitted,the data entered into it by the application is passed to the form processing script(reg.php) via the POST method.The next listing shows the contents of this script:
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<!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" xml:lang="en" lang="en">
<head>
   <title>Member Registration</title>
</head>
<body>
<h2></h2>
<?php
//retrieve details from POST submission
$name=$POST['name'];
$address=$_POST['address'];
$age=$_POST['age'];
$profession=$_POST['profession'];
$resident=$_POST['resident'];
//validate sumbitted data
//check name
if(empty($name)){
die('ERROR:Please provide your name.');
}
//check address
if(empty$address)){
die('ERROR:Please provide your address.');
}
//check age
if(empty($age)){
die('ERROR:Please provide your age.');
}elseif($age<18||$age=>60){
die('ERROR:Membership is only open to those between 18 and 60 years.');
}
//check profession
if(empty($profession)){
die('ERROR:Please provide your profession.')
}
//check residential status
if(strcmp($residrnt,'no')==0){
die('ERROR:Membership is only open to residents.');
}
//if we get this far
//all the input has passed validation
//formulate and send e-mail message
$to='registration@some.domain.com';
$from='webmaster@some.domain.com';
$subject='Application for menbership';
$body="name:$name/r/nAddress:$address/r/n
Age:$age/r/nProfession:$profession/r/n
Residential status:$resident/r/n";
if(mail($to,$subjecct,$body,"From:$from")){
    echo 'Thx for your applicatiion.';
}else{
    die('ERROR:Mail delivery error');
}
?>
</body>
</html>

NOTE
Remember to alter the value of the $to variable in the register.php script,to reflect your e-mail address.

This script begins by retrieving the values submitted by the user from $_POST and assigning these values to regular PHP variables.Next,the empty() function is used to test whether they are empty;those which are,generate an error message and cause the script to terminate immediately.Two extra conditional tests are also present in this section:the first for the applicant's age,to filter out applicants younger than 18 or older than 60;and the second for the applicant's residential status,to filter out non-residents.
Assuming all the checks are successful,the script proceeds to create an e-mail message,setting variables for the sender,recipient,message subject,and message body.
These variables are then passed to PHP's mail() function,which actually does the hard work of sending the e-mail message.If message transmission is successful,a success notification appears;if not,an error message is generated.
The mail() function is new to you and so deserves a more detailed examination.The mail() function is a built-in PHP function to send an e-mail message,and it accepts four parameters:the recipient e-mail address,the message subject,the message body,and a list of additional message headers(of which the 'From' header is mandatory).It uses these parameters to construct an e-mail message,connect to the specified mail server,aand hand the message over to delivery.If handover is successful,mail() returns true;otherwise,it returns false.

CAUTION
It's important to understand what the return value of the mail() function means.If the mail() function returns true,it merely means is that the message was successfully handed over to the mail server for delivery.It does not mean that the message was subsequently successfully transmitted to,or received by,the intended recipient(because PHP has no way of tracking the message once it's been handed over to the mail server).Failing to understand this distinction is a common error made by programmers new to PHP.

Also new in this script is the die() function:this function provides a convenient way to immediately terminate script processing,usually if an erroe occurs.You can also pass an optional message to die();
this message will be output by PHP at the point of script termination aand thus serves as a useful explanation to the user about what went wrong.

More about mail().The fourth argument to the mail() function is a string containing your custom header,in header:value format.If you have more than one custom header to add,separate the headers with
/r/n.The following example illustrates:

<?php
//define message
$to='bacchus@vsnl.com';
$subject='Hello';
$body='This is a test';
//define custom headers
$headers="From:webmaster@my.domain.com/r/n
Organization:MyOrg Inc./r/nX-Mailer:PHP";
if(mail($to,$subject,$body,$headers)){
    echo 'Your message was sent.';
}else{
    die('ERROR:Mail delivery error')
}
?>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值