0

名前、電子メール、メッセージ用のテキスト領域を備えた連絡フォームがあります。

検証に必要な唯一の領域は、電子メールの領域です。

メールのバリデーションが受理されたときに、ネームエリアの内容を示すメッセージとメッセージが送信されたポップアップを表示したい。

検証なしで名前領域をエコーするphpを書くにはどうすればよいですか?

これは、名前と電子メールの両方の検証を含むコードです。電子メールの検証を保持し、名前の検証をクリアしたいのですが、渡されたときに名前をエコーし​​ますか?

<?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 に答える