首页 > 解决方案 > POST方法不适用于联系论坛

问题描述

我试图让 id="RecipientInput" 来处理我的 PHP 代码,但它似乎不起作用。在我这样做之后,我还想添加 reCAPTCHA,但我认为我是一个失败的原因。如果我无法弄清楚这一点,我就无法弄清楚如何从谷歌实现反垃圾邮件 API。

HTML

<form name="contactForm" id="contactForm" method="post" action="" novalidate="novalidate">
                    <fieldset>

                    <div class="form-field">
                        <input name="contactName" type="text" id="contactName" placeholder="Your Name" value="" minlength="2" required="" aria-required="true" class="full-width">
                    </div>
                    <div class="form-field">
                        <input name="contactEmail" type="email" id="contactEmail" placeholder="Your Email" value="" required="" aria-required="true" class="full-width">
                    </div>
                    <div class="form-field">
                        <input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="" class="full-width">
                    </div>
                    <div class="form-field cl-custom-select">
                      <select class="full-width" id="RecipientInput">
                        <option value="Option 1">General Enquiry</option>
                        <option value="Option 2">Pivot</option>
                        <option value="Option 3">Napkin</option>
                      </select>
                    </div>
                    <div class="form-field">
                        <textarea name="contactMessage" id="contactMessage"placeholder="Your Message" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
                    </div>
                    <div class="form-field">
                        <button class="full-width btn-primary">Submit</button>
                        <div class="submit-loader">
                            <div class="text-loader">Sending...</div>
                            <div class="s-loader">
                                <div class="bounce1"></div>
                                <div class="bounce2"></div>
                                <div class="bounce3"></div>
                            </div>
                        </div>
                    </div>

                    </fieldset>
                </form>

PHP

<?php

// Replace this with your own email address
$siteOwnersEmail = 'my email address';


if($_POST) {

    $name = trim(stripslashes($_POST['contactName']));
    $email = trim(stripslashes($_POST['contactEmail']));
    $subject = trim(stripslashes($_POST['contactSubject']));
    // $selected_option = trim(stripslashes($_POST['RecipientInput']));
    $contact_message = trim(stripslashes($_POST['contactMessage']));

    // Check Name
    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name.";
    }
    // Check Email
    if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address.";
    }
    // Check Message
    if (strlen($contact_message) < 15) {
        $error['message'] = "Please enter your message. It should have at least 15 characters.";
    }
    // Subject
    if ($subject == '') { $subject = "Contact Form Submission"; }


    // Set Message
    $message .= "<strong>Email from: </strong>" . $name . "<br />";
    $message .= "<strong>Email address: </strong>" . $email . "<br />";
    // $message .= "<strong>Selected Option: </strong>" . $selected_option . "<br />";
    $message .= "<strong>Message: </strong><br />";
    $message .= $contact_message;
    $message .= "<br /> ----- <br /> This email was sent from www.example.com </a>contact form. <br />";

    // Set From: header
    $from =  $name . " <" . $email . ">";

    // Email Headers
    $headers = "From: " . $from . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


    if (!$error) {

        ini_set("sendmail_from", $siteOwnersEmail); // for windows server
        $mail = mail($siteOwnersEmail, $subject, $message, $headers);

        if ($mail) { echo "OK"; }
        else { echo "Something went wrong. Please try again."; }

    } # end if - no validation error

    else {

        $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
        $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
        $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;

        echo $response;

    } # end if - there was a validation error

}

?>

我正在尝试在我的测试服务器上连续测试,但它目前给我带来了问题。

标签: phphtml

解决方案


您还没有name为选择定义属性,name属性应该在那里

代替

<select class="full-width" id="RecipientInput">

<select class="full-width" id="RecipientInput" name="RecipientInput">

屏幕截图 在此处输入图像描述


推荐阅读