php.1 表单处理
php实现简单表单代码
$message_name=$message_email="";
$m_name=$m_email=true;
$name=$email="";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
if(empty($_POST["name"])){
$message_name="姓名是必填的";
$m_name=false;
}else{
$name=text_input($_POST["name"]);
if(!preg_match("/^[0-9a-zA-Z]*$/",$name))
{
$message_name="姓名只能是数字和字母";
$m_name=false;
}
}
if(empty($_POST["name"])){
$message_email="电子邮箱是必填的";
$m_email=false;
}else{
$email=text_input($_POST["email"]);
if(!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$message_email="电子邮箱格式不正确";
$m_email=false;
}
}
}
function text_input($data){
$data=trim($data);
$data=stripcslashes($data);
$data=htmlspecialchars($data);
return $data;
}
?>
span.dx
{
color:#FF0000;
}
附:
// 模式定界符后面的 "i" 表示不区分大小写字母的搜索
if (preg_match ("/hi/i", "Welcome to hi-docs.com.")) {
echo "A match was found.";
} else {
echo "A match was not found.";
}
?>