两个表单和按钮bootstrap ajax

时间:2017-04-01 15:49:10

标签: javascript php jquery forms twitter-bootstrap

我的网页出现问题。它包含在HTML,Bootstrap和PHP中。

我在HTML页面中有两个表单。首先是联系表格,另一个是模态,这是分发形式。

问题是分发表单中的数据,因为当我点击按钮时,它只发送联系表单而不是分发表单。

这是代码。

分发表格:

<div class="col-lg-12 animated slideInUp">
        <form name="sentMessage" action="contact_me.php"  id="contactForm2" novalidate>
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="NOMBRE *" id="name" required data-validation-required-message="Por favor introduce tu nombre.">
                        <p class="help-block text-danger"></p>
                    </div>
                    <div class="form-group">
                        <input type="email" class="form-control" placeholder="EMAIL *" id="email" required data-validation-required-message="Por favor introduce tu E-mail.">
                        <p class="help-block text-danger"></p>
                    </div>
                    <div class="form-group">
                        <input type="tel" class="form-control" placeholder="TELEFONO *" id="phone" required data-validation-required-message="Por favor introduce tu numero de Telefono.">
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <textarea class="form-control" style="height:135px;" placeholder="TU MENSAJE *" id="message" required data-validation-required-message="Por favor introduce tu mensaje."></textarea>
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="clearfix"></div>
                <div class="col-lg-12 text-center">
                    <div id="success"></div>
                    <button type="submit" class="btn btn-primary">UNIRME AL EQUIPO</button>
                </div>
            </div>
        </form>
    </div>
</div>

联系表格:

<div class="col-lg-12">
    <form name="sentMessage" action="contact_me.php" id="contactForm" novalidate>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="NOMBRE *" id="name" required data-validation-required-message="Por favor introduce tu nombre.">
                    <p class="help-block text-danger"></p>
                </div>
                <div class="form-group">
                    <input type="email" class="form-control" placeholder="EMAIL *" id="email" required data-validation-required-message="Por favor introduce tu E-mail.">
                    <p class="help-block text-danger"></p>
                </div>
                <div class="form-group">
                    <input type="tel" class="form-control" placeholder="TELEFONO *" id="phone" required data-validation-required-message="Por favor introduce tu numero de Telefono.">
                    <p class="help-block text-danger"></p>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <textarea class="form-control" placeholder="TU MENSAJE *" id="message" required data-validation-required-message="Por favor introduce tu mensaje."></textarea>
                    <p class="help-block text-danger"></p>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="col-lg-12 text-center">
                <div id="success"></div>
                <button type="submit" class="btn btn-primary">Enviar Mensaje</button>
            </div>
        </div>
    </form>
</div>

JavaScript contact_me:

$(function() {

    $("input,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>Tu mensaje ha sido enviado. </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");
    });
});

1 个答案:

答案 0 :(得分:0)

问题是由于您在两种表单中都拥有相同的ID名称而引起的。保持每个页面的id名称是唯一的(作为一个原则和良好实践),但如果你不这样做,jQuery会抓住第一个匹配的id - 这就是为什么只有第一个表单的输入是提交。

我建议将第二个表单的ID更改为name_1,email_1,phone_1和message_1(使其唯一),然后在脚本中替换:

// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();

使用:

// Determine which form is being submitted
var form_0 = $form.attr('id') === 'contactForm2' ? true : false;

// If it's the first form, assign the values from that form,
// otherwise assign the values from the second form
var name = form_0 ? $('#name').val() : $('#name_1').val();
var email = form_0 ? $('#email').val() : $('#email_1').val();
var phone = form_0 ? $('#phone').val() : $('#phone_1').val();
var message = form_0 ? $('#message').val() : $('#message_1').val();

然后使用适当的表格提交正确的值。

希望这有帮助。

如果您不熟悉“三元”杂音。上面代码中的运算符 - 它们只是if {} else {}语句的便捷快捷方式 - 请查看:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator