php在查询/回调表单上无法正常工作,但在联系表单上工作正常

时间:2018-03-28 12:07:07

标签: php html

这让我感到困惑的是,在提交表单时,为什么我只收到电话号码(缺少姓名和电子邮件字段)?我试过改变一切,但我似乎无法让其他两个字段在提交时起作用。

  <aside class="sidebar">
    <h3 class="text-center">Request a Callback</h3>

    <div class="enquiry-wrapper">
      <form id="enquiry-form" method="post" action="enquiry.php" class="clearfix">
        <input type="text" name="enquiry-name" id="enquiry-name" autocomplete="off" placeholder="Name ...">

        <input type="email" name="enquiry-email" id="enquiry-email" autocomplete="off" placeholder="Email ...">

        <input type="tel" name="tel" id="tel" autocomplete="off" placeholder="Phone Number ...">

        <div class="form-submit">
          <input type="submit" class="btn btn-colour" name="enquiry-submit" id="enquiry-submit" value="Send message">
        </div>
        <!-- form-submit end -->
      </form>
      <div id="enquiry-message"></div>
    </div>
    <!-- enquiry-wrapper	end -->
  </aside>

这是enquiry.php

    <?php
/*
 *  CONFIGURE EVERYTHING HERE
 */

// an email address that will be in the From field of the email.
$from = 'contact@rpsfm.co.uk';

// an email address that will receive the email with the output of the form
$sendTo = 'contact@rpsfm.co.uk';

// subject of the email
$subject = 'new contact form';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('enquiry-name' => 'Name', 'enquiry-email' => 'Email', 'tel' => 'Tel');

// message that will be displayed when everything is OK :)
$okMessage = header( 'Location: http://rpsfm.co.uk/thanks1.html' );

// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';

/*
 *  LET'S DO THE SENDING
 */

// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $emailText = "You have a new message from your contact form\n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $from,
        'Return-Path: ' . $from,
    );

    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}
?>

这是js。

jQuery(document).ready(function() {
    "use strict";
    $("#enquiry-form").submit(function() {
        var e = $(this).attr("action");
        $("#enquiry-message").slideUp(750, function() {
            $("#enquiry-message").hide();
            $("#enquiry-submit").after("").attr("disabled", "disabled");
            $.post(e, {
                name: $("#enquiry-name").val(),
                email: $("#enquiry-email").val(),
                tel: $("#tel").val()
            }, function(e) {
                document.getElementById("enquiry-message").innerHTML = e;
                $("#enquiry-message").slideDown("slow");
                $("#enquiry-form img.loader").fadeOut("slow", function() {
                    $(this).remove()
                });
                $("#enquiry-submit").removeAttr("disabled");
                if (e.match("success") != null)
                    $("#enquiry-form").slideUp("slow")
            })
        });
        return false
    })
});

希望我能做到这一点,如果不让我知道你需要的任何信息。

ATB 路加

1 个答案:

答案 0 :(得分:0)

有许多让我惊讶的事情,除此之外,我不明白你想要完成什么。

在enquiry.php中:

// message that will be displayed when everything is OK :)
$okMessage = header( 'Location: http://rpsfm.co.uk/thanks1.html' );

从手册中摘录:

  

第二个特例是&#34;位置:&#34;头。不仅如此   将此标头发送回浏览器,但它也会返回REDIRECT   (302)状态代码到浏览器,除非201或3xx状态代码   已经确定了。

通过将此传递给变量,您在执行代码之前几乎立即重定向。

其次,您的JavaScript代码似乎并不像您希望的那样工作。我想你想通过AJAX发送表单,但你的代码会被忽略,因为表单是通过普通邮件发送的。

event.preventDefault();应阻止通过常规提交发送表单。

一样使用
jQuery(document).ready(function() {
    "use strict";
    $("#enquiry-form").submit(function(event) {
        event.preventDefault();

        // [...]

    });
});

随着我遇到更多问题,我会更新。

<强>更新

请看一下:

$(document).ready(function() {
    "use strict";
    $("#enquiry-form").submit(function(event) {
        event.preventDefault();
        var e = $(this).attr("action");
        $("#enquiry-message").slideDown(750, function() {
            $("#enquiry-message").hide();
            $("#enquiry-submit").after("").attr("disabled", "disabled");
            $.post(e, {
                "enquiry-name": $("#enquiry-name").val(),
                "enquiry-email": $("#enquiry-email").val(),
                "tel": $("#tel").val()
            }, function(e) {
                document.getElementById("enquiry-message").innerHTML = e;
                $("#enquiry-message").slideDown("slow");
                $("#enquiry-form img.loader").fadeOut("slow", function() {
                    $(this).remove();
                });
                $("#enquiry-submit").removeAttr("disabled");

                if (e.type === "success") {
                    $("#enquiry-form").slideUp("slow");
                }
            });
        });
    });
});

您为POST数据使用了错误的变量名称。因此,enquiry.php中的关联数组无法使用正确的密钥并将其遗漏。似乎header()函数的赋值有效,但对我来说这似乎是非常糟糕的做法。您可以使用AJAX返回调用返回ok消息,而不是使用header()函数。