无法在PHP表单中添加变量

时间:2015-10-31 23:16:21

标签: javascript php contact-form

我是PHP的新手,所以如果这是一个非常明显的问题,请道歉。我遇到的问题是我想创建一个表单,收集一些额外的细节,而不是传统的“名称”“电子邮件”和“消息”。

具体来说,我想要以下标签;

(i)你会参加吗? (ii)你的名字 (iii)您的电子邮件 (iv)您的客人姓名 (v)消息

这是我到目前为止所拥有的

<form name="sentMessage" id="contactForm" novalidate>

<div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Attendance</label>
                                <input type="text" class="form-control" placeholder="Will you be attending?" id="going" required data-validation-required-message="Please type [Yes] or [No]">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>

                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Your Name</label>
                                <input type="text" class="form-control" placeholder="Your name" id="name" required data-validation-required-message="Please enter your name.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>

                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Email Address</label>
                                <input type="email" class="form-control" placeholder="Your email Address" id="email" required data-validation-required-message="Please enter your email address.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>

                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls">
                                <label>Your Name</label>
                                <input type="text" class="form-control" placeholder="Your guests name" id="guest" required data-validation-required-message="Please enter your guests name. Type 'none' if none.">
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>

                        <div class="row control-group">
                            <div class="form-group col-xs-12 floating-label-form-group controls" id="message">
                                <label>Any dietary requirements</label>
                                <textarea rows="5" class="form-control" placeholder="Have you any dietary requirements?" id="message" required data-validation-required-message="Please enter a message."></textarea>
                                <p class="help-block text-danger"></p>
                            </div>
                        </div>
                        <br>
                        <div id="success"></div>
                        <div class="row">
                            <div class="form-group col-xs-12">
                                <button type="submit" class="btn btn-outline-dark">SEND YOUR REPLY</button>
                            </div>
                        </div>
                    </form>

和PHP

$name = $_POST['name'];
$email_address = $_POST['email'];
$guestname = $_POST['guestname'];
$message = $_POST['message'];

和JS

$(function() {

$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
        // additional error messages or events
    },
    submitSuccess: function($form, event) {
        event.preventDefault(); // prevent default submit behaviour
        // get values from FORM
        var name = $("input#name").val();
        var email = $("input#email").val();
        var phone = $("input#phone").val();
        var message = $("textarea#message").val();
        var firstName = name; // For Success/Failure Message
        // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
            firstName = name.split(' ').slice(0, -1).join(' ');
        }
        $.ajax({
            url: "contact_me.php",
            type: "POST",
            data: {
                name: name,
                phone: phone,
                email: email,
                message: message
            },
            cache: false,
            success: function() {
                // Success message
                $('#success').html("<div class='alert alert-    success'>");
                $('#success > .alert-success').html("<button     type='button' class='close' data-dismiss='alert' aria-    hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-success')
                    .append("<strong>Your message has been sent. </strong>");
                $('#success > .alert-success')
                    .append('</div>');

                //clear all fields
                $('#contactForm').trigger("reset");
            },
            error: function() {
                // Fail message
                $('#success').html("<div class='alert alert-danger'>");
                $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-danger').append("<strong>Sorry " +      firstName + ", it seems that my mail server is not responding. Please try again later!");
                $('#success > .alert-danger').append('</div>');
                //clear all fields
                $('#contactForm').trigger("reset");
            },
        })
    },
    filter: function() {
        return $(this).is(":visible");
    },
});

$("a[data-toggle=\"tab\"]").click(function(e) {
    e.preventDefault();
    $(this).tab("show");
});

});

/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
$('#success').html('');
});

当我提交表单时,我会收到有关姓名,电子邮件和消息的信息 - 但我错过了“客人姓名”和“您将参加”的详细信息

关于我做错的任何想法?

2 个答案:

答案 0 :(得分:0)

我可以在您提交给我们的代码中看到至少两个明显的错误。首先,

$guestname = $_POST['guestname']

您的HTML表单输入

id="guest"

而不是&#39; guestname&#39;。改变它们以使它们匹配。

并且你的php代码缺少id =&#34; go&#34; POST

因此应该是:

$name = $_POST['name'];
$email_address = $_POST['email'];
$guestname = $_POST['guest'];
$message = $_POST['message'];

$isAttending = $_POST['going'] /* $isAttending or something along those lines */

答案 1 :(得分:0)

(您已为访客姓名

创建了一个类型
$guestname = $_POST['guest'];

然后你没有告诉我们你如何检查“你会参加,所以我猜

$attending = $_POST['going'];