PHP提交按钮不将HTML表单信息发送到电子邮件

时间:2014-12-16 20:09:11

标签: php html forms

我不太确定我的代码有什么问题,我的提交按钮不会将信息从我的表单发送到我的电子邮件,有没有人知道什么是错误/我应该添加什么。从查看其他论坛看起来是正确的,但我又是PHP和编码的新手,并且非常感谢正确的帮助/代码来完成这个项目。谢谢大家,代码片段如下:

HTML FOR THE PAGE --->
<!DOCTYPE html>
<html>
        <head>
        <link rel="stylesheet" href="style.css" type="text/css"/>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="script.js"></script>
        </head>
        <body>
                <div id="header">
                        <img class="logo" src="logo.png" style="height: 90px; width: 160px; position: relative; top: -30px; left: 80px;"><a href="#"></a></img>
                        <div id="nav">
                                <ul>
                                        <li><a href="index.php">Home</a></li>
                                        <li class="active"><a href="donate.html">Donate</a></li>
                                        <li><a href="get.html">Get</a></li>
                        </div>
                        <h1 class="intro" style="display:none;"> Wanna Donate your Netflix Account? Do so here... </h1>
                        <p class="intro2" style="display:none;"> Whilst we do provide free netflix accounts,they are supplied from somewhere</p>
                        <p class="ltd" style="display:none;"> We can only supply these netflixes with your help, and your donations!</p>
                </div>
                <form name="contactform" method="post" action="send_form_email.php">

                        <table width="450px">

                                <tr>

                                        <td valign="top">

                                                <label for="first_name">First Name *</label>

                                        </td>

                                        <td valign="top">

                                                <input  type="text" name="first_name" maxlength="50" size="30">

                                        </td>

                                </tr>

                                <tr>

                                        <td valign="top">

                                                <label for="last_name"> Last Name *</label>

                                        </td>

                                        <td valign="top">

                                                <input  type="text" name="last_name" maxlength="50" size="30">

                                        </td>

                                </tr>

                                <tr>

                                        <td valign="top">

                                                <label for="email">Email Address *</label>

                                        </td>

                                        <td valign="top">

                                                <input  type="text" name="email" maxlength="80" size="30">

                                        </td>

                                </tr>

                                <tr>

                                        <td valign="top">

                                                <label for="comments">Accounts *</label>

                                        </td>

                                        <td valign="top">

                                                <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

                                        </td>

                                </tr>

                                <tr>

                                        <td colspan="2" style="text-align:center">

                                                <input type="submit" value="Submit">   <a href="#">Email Form</a>

                                        </td>

                                </tr>

                        </table>

                </form>

        </body>
</html>
</code>
PHP FOR THE FORM ON THE PAGE ---------------->

<?php

if(isset($_POST['email'])) {



  // EDIT THE 2 LINES BELOW AS REQUIRED

  $email_to = "cerbolt@gmail.com";

  $email_subject = "Netflix Donation Site";





  function died($error) {

    // your error code can go here

    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['first_name']) ||

  !isset($_POST['last_name']) ||

  !isset($_POST['email']) ||

  !isset($_POST['comments'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');

  }



  $first_name = $_POST['first_name']; // required

  $last_name = $_POST['last_name']; // required

  $email_from = $_POST['email']; // required

  $comments = $_POST['comments']; // required



  $error_message = "";

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

  if(!preg_match($email_exp,$email_from)) {

    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';

  }

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

  if(!preg_match($string_exp,$first_name)) {

    $error_message .= 'The First Name you entered does not appear to be valid.<br />';

  }

  if(!preg_match($string_exp,$last_name)) {

    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';

  }

  if(strlen($comments) < 2) {

    $error_message .= 'The Comments you entered do not appear to be valid.<br />';

  }

  if(strlen($error_message) > 0) {

    died($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 .= "First Name: ".clean_string($first_name)."\n";

  $email_message .= "Last Name: ".clean_string($last_name)."\n";

  $email_message .= "Email: ".clean_string($email_from)."\n";

  $email_message .= "Comments: ".clean_string($comments)."\n";





  // create email headers

  $headers = 'From: '.$email_from."\r\n".

  'Reply-To: '.$email_from."\r\n" .

  'X-Mailer: PHP/' . phpversion();

  @mail($email_to, $email_subject, $email_message, $headers);

  ?>



  <!-- include your own success html here -->

  <script language="javascript" type="text/javascript">
alert('Thank you for your donations, most appreciated!');
window.location = 'index.php';
</script>


  <?php

}

?>;

0 个答案:

没有答案