HTML表单在提交时发布PHP文件而不是发送电子邮件

时间:2015-11-04 10:22:23

标签: php html forms email

以下是表单的HTML代码,并且它是用于将数据接收到所需电子邮件地址的php代码。在提交时,它会重定向到php代码而不是发送邮件。 提前致谢。

     <!doctype html>
                <html>
                <head>
                <meta charset="utf-8">
                <title>HTML Form</title>
                <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
            <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
                <script>
                  $(document).ready(function () {
                  $("#htmlform").submit(function (e) {
                  var url = "html_form_send.php"; // the script where you handle the form input.
                  $.ajax({type: "POST",
                   url: url,
                   data: $(this).serialize(), // serializes the form's elements.
                      success: function (data) {
                          alert(data); // show response from the php script.
                      }});
                  e.preventDefault(); // avoid to execute the actual submit of the form
                    });
                    });
              </script>
                </head>
                <form name="htmlform" id="htmlform" method="post" action="html_form_send.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="telephone">Telephone Number</label>
                 </td>
                 <td valign="top">
                  <input  type="text" name="telephone" maxlength="30" size="30">
                 </td>
                </tr>
                <tr>
                 <td valign="top">
                  <label for="comments">Comments *</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 class="Submit" type="submit" name="Submit" value="Submit"/>**
                  </td>
                </tr>
                </table>
                </form> 

                </body>
                </html>

PHP代码

           <?php
        if ( isset( $_POST['first_name'] ) ) { // <<<< Changes i have made

          $email_to = "Name@gmail.com"; // <<<<<<< This is temporary. 


                $email_subject = "website html form submissions";


                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['telephone']) ||
                    !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
                $telephone = $_POST['telephone']; // not 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 .= "Telephone: ".clean_string($telephone)."\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 );


      }
      ?>

2 个答案:

答案 0 :(得分:2)

使用ajax发布您的表单,$("#htmlform").submit(function (e) {.... 将避免重定向。我认为这个评论太长了,但我引用了你的评论。

您只是忘记在代码中将表单更改为id:

<强> JavaScript的:

<form name="htmlform" id="htmlform"....

<强> PHP:

var url = "path/to/your/html_form_send.php";// insert your path to the php file

您必须在以下示例中进行更改:

<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
    $(document).ready(function () {
        $("#htmlform").submit(function (e) {
            var url = "path/to/your/html_form_send.php"; // the script where you handle the form input.
            $.ajax({type: "POST", url: url, data: $(this).serialize(), // serializes the form's elements.
                success: function (data) {
                    alert(data); // show response from the php script.
                }});
            e.preventDefault(); // avoid to execute the actual submit of the form
        });
    });
</script>
<form name="htmlform" id="htmlform" method="post" action="html_form_send.php">
    <table width="450px">
        <!-- just removed for testing -->
        <tr>
            <td colspan="2" style="text-align:center">
                <input type="submit" name="Submit" value="Submit">
            </td>
        </tr>
    </table>
</form>

这是一个有效的例子:

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

    $email_to = "Name@gmail.com";

    /**
     *  JUST SHORTENED FOR THIS EXAMPLE
     */
    // save the return of the mail function, if you are using the @
    $sent = @mail( $email_to, $email_subject, $email_message, $headers );

    echo json_encode( array( "success" => $sent ) ); // return something that you can use in javascript
}// <<<<<<< YOU FORGOT THIS
?>

<强> html_form_send.php

if

<强>提示: 来自jQuery docs

  

注意:只有&#34;成功控制&#34;被序列化为字符串。没有   由于表单未提交,因此提交按钮值已序列化   使用按钮。

因为你使用序列化

,所以你的if ( isset( $_POST['Submit'] ) ) { 将永远不会成真
if ( isset( $_POST['first_name'] ) ) {

只需将其更改为

即可
<form name="htmlform" id="htmlform" method="post" action="html_form_send.php">
        <input type="hidden" name="Submit" value="some value" />

或添加一个名为&#34的隐藏输入字段;提交&#34;到您的表格

{{1}}

答案 1 :(得分:0)

如果您提交表单,则会收到您在action参数中指定的文档。

如果你想将数据发送到你的php而不加载到不同的页面你可以使用ajax调用,你可以通过javascript调用buttonclick,或者只是简单地将你的php与你的html文档合并并引用一个&#39 ;#&#39;在action参数中。