简体   繁体   English

联系表格,发送 email 只是在下载我的 PHP 文件

[英]Contact Form, sending an email is just downloading my PHP file

I'm having a weird error that I cannot figure out with a PHP file, I've never touched PHP and I am trying to use it to send a Name, Email Address, and a message back to me from a website, but for some reason, the code I wrote is just downloading the PHP file instead, can anyone else see where I may be going wrong here:我有一个奇怪的错误,我无法用 PHP 文件弄清楚,我从来没有接触过 PHP,我正试图用它来发送一个名称,Email 地址,但来自一个网站的消息,出于某种原因,我编写的代码只是下载了 PHP 文件,其他人可以看到我在这里可能出错的地方:

HTML HTML

<div class="modal-body">
      <div class="container"> <!-- TODO add in contact form -->
        <form action="form-to-action.php">
      
          <label for="fname">Name</label>
          <input type="text" id="Name" name="Name" placeholder="Name">
      
          <label for="lname">Email Address</label>
          <input type="text" id="Email" name="Email" placeholder="Email Address">
          <br>
      
          <label for="subject">How can we Help?</label>
          <textarea id="Message" name="Message" placeholder="Write something.." style="height:200px"></textarea>
      
          <input type="submit" value="Send"id="contactBtnModal"style="text-decoration: none;" >
      
        </form>
      </div>
    </div>

PHP: PHP:

     <?php
    if (isset($_POST['Email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "testingemail@gmail.com";
    $email_subject = "Testing";

    function problem($error)
    {
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br><br>";
        echo $error . "<br><br>";
        echo "Please go back and fix these errors.<br><br>";
        die();
    }

    // validation expected data exists
    if (
        !isset($_POST['Name']) ||
        !isset($_POST['Email']) ||
        !isset($_POST['Message'])
    ) {
        problem('We are sorry, but there appears to be a problem with the form you submitted.');
    }

    $name = $_POST['Name']; // required
    $email = $_POST['Email']; // required
    $message = $_POST['Message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

    if (!preg_match($email_exp, $email)) {
        $error_message .= 'The Email address you entered does not appear to be valid.<br>';
    }

    $string_exp = "/^[A-Za-z .'-]+$/";

    if (!preg_match($string_exp, $name)) {
        $error_message .= 'The Name you entered does not appear to be valid.<br>';
    }

    if (strlen($message) < 2) {
        $error_message .= 'The Message you entered do not appear to be valid.<br>';
    }

    if (strlen($error_message) > 0) {
        problem($error_message);
    }

    $email_message = "Form details below.\n\n";

    function clean_string($string)
    {
        $bad = array("content-type", "bcc:", "to:", "cc:", "href");
        return str_replace($bad, "", $string);
    }

    $email_message .= "Name: " . clean_string($name) . "\n";
    $email_message .= "Email: " . clean_string($email) . "\n";
    $email_message .= "Message: " . clean_string($message) . "\n";

    // create email headers
    $headers = 'From: ' . $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $headers);
    ?>

    <!-- include your success message below -->

    Thank you for contacting us. We will be in touch with you very soon.

<?php
}
?>

Are you installed a PHP server in your Computer?您是否在计算机中安装了 PHP 服务器?

If not, download WAMP and try again, you need access the page via http://localhost, and not via file://path_to_document in your web navigator.如果没有,请下载 WAMP 并重试,您需要通过 http://localhost 访问该页面,而不是通过 web 导航器中的 file://path_to_document 访问该页面。

Or run the script in a web server.或者在 web 服务器中运行脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM