0

I have a contact-form with text-areas for name, e-mail and the message.

The only area i want required for validation is the area for e-mail.

When validation for e-mail is accepted, i want to show a message showing the content of the name-area and a pop-up that message is sent.

How do i write php that echoes the name-area without validation ?

Here is the code with validation for both name- and e-mail - i want to keep validation for e-mail and clear validation for name but still echo name when passed ?

<?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;
}
 }
?>
4

2 回答 2

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...
}
?>
于 2013-09-05T16:29:40.140 回答
0

如果您只想回显 name 的值(如果它不为空),只需替换这部分代码:

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
} 
于 2013-09-05T16:08:44.190 回答