简体   繁体   English

使用PHP AJAX显示成功消息而不显示页面刷新后,再次显示Form div

[英]Show Form div again after displaying success message without Page Refresh Using PHP AJAX

i am newbie to php and ajax. 我是php和ajax的新手。 I want to submit a form without page refresh. 我想提交不刷新页面的表单。 Which i achieved Successfully. 我成功实现的目标。 the form after submission displays the success message. 提交后的表单显示成功消息。 But i want that after success message. 但是,我要在成功消息后得到。 The result fades Out and shows the empty form fields again. 结果淡出并再次显示空白表单字段。 so that i can submit anouther form again. 这样我就可以再次提交一份表格。

<script src="assets/jquery-1.12.4-jquery.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {  

// submit form using $.ajax() method

$('#reg-form').submit(function(e){

    e.preventDefault(); // Prevent Default Submission

    $.ajax({
        url: 'submit.php',
        type: 'POST',
        data: $(this).serialize() // it will serialize the form data
    })
    .done(function(data){
        $('#form-content').fadeOut('slow', function(){
            $('#form-content').fadeIn('slow').html(data);
            $('#form-content').fadeOut('slow').html(data);
            $('#form-content').fadeIn('slow').html();

        });
    })
    .fail(function(){
        alert('Ajax Submit Failed ...');    
    });
});

}); });

<div id="form-content">

        <form method="post" id="reg-form" autocomplete="off">

            <div class="form-group">
                <input type="text" class="form-control" name="txt_fname" id="lname" placeholder="First Name" required />
            </div>

            <div class="form-group">
                <input type="text" class="form-control" name="txt_lname" id="lname" placeholder="Last Name" required />
            </div>

            <div class="form-group">
                <input type="text" class="form-control" name="txt_email" id="lname" placeholder="Your Mail" required />
            </div>

            <div class="form-group">
                <input type="text" class="form-control" name="txt_contact" id="lname" placeholder="Contact No" required />
            </div>

            <hr />

            <div class="form-group">
                <button class="btn btn-primary">Submit</button>
            </div>

        </form>

        </div>

submit.php submit.php

if( $_POST ){

$fname = $_POST['txt_fname'];
$lname = $_POST['txt_lname'];
$email = $_POST['txt_email'];
$phno = $_POST['txt_contact'];

?>
<table class="table table-striped" border="0">

<tr>
<td colspan="2">
    <div class="alert alert-info">
        <strong>Success</strong>, Form Submitted Successfully...
    </div>
</td>
</tr>

<tr>
<td>First Name</td>
<td><?php echo $fname ?></td>
</tr>

<tr>
<td>Last Name</td>
<td><?php echo $lname ?></td>
</tr>

<tr>
<td>Your eMail</td>
<td><?php echo $email; ?></td>
</tr>

<tr>
<td>Contact No</td>
<td><?php echo $phno; ?></td>
</tr>

</table>
<?php

} ?> }?>

please help me out.you can see the working example here http://demos.codingcage.com/ajax-form-submit/ 请帮帮我。您可以在此处查看工作示例http://demos.codingcage.com/ajax-form-submit/

First, you need to modify the ID attributes in the form, as ID have to be unique. 首先,您需要修改表单中的ID属性,因为ID必须是唯一的。 Then, when submit is successful, you can use the success event jQuery DOC . 然后,当提交成功时,您可以使用success事件jQuery DOC

All this assuming that the PHP side is fine, as you mentionned it. 正如您所提到的,所有这些都假设PHP方面很好。

HTML HTML

<div id="response"></div>
<div id="form-content">
    <form method="post" id="reg-form" autocomplete="off">
        <div class="form-group">
            <input type="text" class="form-control" name="txt_fname" id="fname" placeholder="First Name" required />
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="txt_lname" id="lname" placeholder="Last Name" required />
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="txt_email" id="email" placeholder="Your Mail" required />
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="txt_contact" id="num" placeholder="Contact No" required />
        </div>
        <hr />
        <div class="form-group">
            <button class="btn btn-primary">Submit</button>
        </div>
    </form>
</div>

I just added a response div, in case you need to let the user know it was successful (not mandatory but user friendly). 我只是添加了一个响应div,以防您需要让用户知道它是成功的(不是强制性的,而是用户友好的)。 I also modified the way you handled the data (see comments in code) 我还修改了您处理数据的方式(请参见代码中的注释)

jQuery (3.2.1) jQuery(3.2.1)

<script type="text/javascript">

function clear_form() // only if you want to clear the form after success
 {  // I use the IDs from form, adapt to yours if needed
    $("#fname").val('');
    $("#lname").val('');
    $("#email").val('');
    $("#num").val('');
 }

$(document).ready( function() {

  $('#reg-form').submit(function(e){

  e.preventDefault(); // Prevent Default Submission
  var data = $("#reg-form").serialize(); // it will serialize the form data
    $.ajax({
    url: 'submit.php',
    type: 'POST',
    data: { data }, // added the { } to protect the data

    success : function(data){ // here we use the success event

    $('#response').html(data); // only if you use PHP response and want to show it

    $('#form-content').fadeOut("slow");
    clear_form(); // reset all fields
    $('#form-content').fadeIn("slow");

    },
    error: function (request, status, error) { // handles error
    alert(request.responseText); // change alert to whatever you want/need
    }
   })
    .fail(function(){
    alert('Ajax Submit Failed ...');    
    });
  });

});

</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM