dsf12313
dsf12313
2014-08-04 12:07

使用ajax在一个页面上发送多个表单数据

I have multiple forms on one web page. I need that user could post data from any form and this data must send to my email. Now working only 1st form.

HTML:

<form name="sentMessage" id="contactForm" novalidate>
        <div>
        <label>Name</label>
        <input type="text" placeholder="Name" class="name">
    </div>
    <div>
        <label>Email</label>
        <input type="email" placeholder="Email" class="email">
    </div> 
    <div id="success"></div>
    <button type="submit">Submit</button>
</form>
<hr>
<form name="sentMessage" id="contactForm" novalidate>
    <div>
        <label>Name</label>
        <input type="text" placeholder="Name" class="name">
    </div>
    <div>
        <label>Email</label>
        <input type="email" placeholder="Email" class="email">
    </div> 
    <div id="success"></div>
    <button type="submit">Submit</button>
</form>
<hr>
<form name="sentMessage" id="contactForm" novalidate>
    <div>
        <label>Name</label>
        <input type="text" placeholder="Name" class="name">
    </div>
    <div>
        <label>Email</label>
        <input type="email" placeholder="Email" class="email">
    </div> 
    <div id="success"></div>
    <button type="submit">Submit</button>
</form>

PHP:

<?php   
$name = $_POST['name'];
$email = $_POST['email'];

// Create the email and send the message
$to = 'my@email.com'; 
$email_subject = "Email from $name";
$email_body = "     Details:

"."
                    Name: $name


                    Email: $email

";
$headers = "Content-type: text/plain; charset=utf-8"; 

    mail($to,$email_subject,$email_body,$headers);
return true;    
?>

JS:

$(function() {

    $("input").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 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: "././mail/contact.php",
                type: "POST",
                data: {
                    name: name,
                    email: email
                },
                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('');
});

What should I change? How can I submit all 3 forms?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • drrkgbm6851 drrkgbm6851 7年前

    You can use single form like:

    <form name="sentMessage" id="contactForm" novalidate>
        <div>
            <label>Name</label>
            <input type="text" name="name[]" placeholder="Name" class="name">
        </div>
        <div>
            <label>Email</label>
            <input type="email" name="email[]" placeholder="Email" class="email">
        </div> 
    <div>
            <label>Name</label>
            <input type="text" name="name[]" placeholder="Name" class="name">
        </div>
        <div>
            <label>Email</label>
            <input type="email" name="email[]" placeholder="Email" class="email">
        </div> 
    
        <div id="success"></div>
        <button type="submit">Submit</button>
    </form>
    

    JS

     $(function() {
    
    $("input").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 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: "././mail/contact.php",
                type: "POST",
                data: $('#contactForm').serialize(),
                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('');});
    
    点赞 评论 复制链接分享
  • dongpu1331 dongpu1331 7年前

    Change all button types from submit to button and add an onClick event to call function, that is

    <input type "button" onclick="savedata()" />

    and the function will be like this:

    <script> function savedata() { .... your ajax request here.... } </script>

    点赞 评论 复制链接分享

相关推荐