我有一个联系表格,其中包含姓名,电子邮件和信息的文字区域。
我想要验证的唯一区域是电子邮件区域。
接受电子邮件验证后,我想显示一条消息,显示名称区域的内容以及发送消息的弹出窗口。
如何编写与未经验证的名称区域相呼应的php?
以下是对名称和电子邮件进行验证的代码 - 我希望继续验证电子邮件并清除名称验证,但在传递时仍然是echo名称?
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//THIS PART SHOULD NOT validate but just echo if no error in e-mail
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = '#@gmail.com'; //Put your own email address here
$body = "Email: $email \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
答案 0 :(得分:0)
如果您只想回显名称的值,如果它不为空,则只需替换此部分代码:
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
有了这个:
if( isset($_POST['contactname']) ) {
echo $_POST['contactname'];//this will print it in your page
}
答案 1 :(得分:0)
这是一个难以理解的问题,所以我不确定这是否是你想要的......
<?php
//If the form is submitted
if($_SERVER['REQUEST_METHOD'] == 'POST') {
//Check to make sure sure that a valid email address is submitted
if($_POST['email'] == '' || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$hasError = true;
} else {
$email = strip_tags(trim($_POST['email']));
$emailTo = '#@gmail.com'; //Put your own email address here
$body = "Email: $email \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
// send the email
$sent = mail($emailTo, $subject, $body, $headers);
}
//THIS PART SHOULD NOT validate but just echo if no error in e-mail
// no point in setting $hasError here if you don't require validation
if($_POST['contactname'] != '') {
$name = strip_tags(trim($_POST['contactname']));
echo $name;
}
// if the email was sent
if($sent) {
echo 'email sent.';
}
// don't do popups, popups are annoying.
// instead, echo the message in a div or something...
}
?>