How to use multiple Submit buttons in a single form having Ajax [closed]
Asked Answered
F

4

6

I'm building a simple website which uses mysql as backend. I have a form which has 2 buttons of which one searches data from db and populates the page using Ajax another button updates the new values to db. Now my problem is i can't use both buttons as 'Submit' button.

Ajax Method & Html Form:

<script type="text/javascript">
     $(document).ready(function(){  
           $(document).on('submit', '#update-form', function(){
                var data = $(this).serialize();
                $.ajax({
                    type : 'POST',
                    url  : 'search.php',
                    data : data,
                    success :  function(data){
                        $(".display").html(data);
                    }
                });
                return false;
            });
        });
</script>
<body>
  <div id="update" class="tab-pane fade">
                <form id="update-form" role="form" class="container" method="post" action="search.php">
                    <h3>Update details</h3>
                    <table class="display">
                        <tr  class="space">
                            <td><label>File Number:</label></td>
                            <td><input type="text" class="form-control" name="filenum" required></td>
                        </tr>
                    </table>
                    <button type="submit" class="btn btn-default text-center" name="search_btn" >Search</button>      
                    <button type="submit" class="btn btn-default text-center"  name="submit_btn" formaction="update.php">Update</button>
                </form>
            </div>
</body>

Now i want to know how can i modify above code so that i can use both button as submit.

Tried withformaction but it doesn't work because of Ajax method usage.

Forrester answered 26/9, 2016 at 12:19 Comment(2)
Why do you need them both to be submit buttons?Ivelisseivens
No more needed. Solution found.Forrester
B
7

You can do this in many ways.

The first that come to my mind is: change the button type to be button instead of submit and add a onclick attribute

<button type="button" onclick="submitForm('search.php')">Search</button>
<button type="button" onclick="submitForm('update.php')">Update</button>

Then in your javascript:

function submitForm(url){
    var data = $("$update-form").serialize();
    $.ajax({
        type : 'POST',
        url  : url,
        data : data,
        success :  function(data){
            $(".display").html(data);
        }
    });
}
Brough answered 26/9, 2016 at 12:43 Comment(1)
Perfect solution. Working great. Thank you!Forrester
N
1

In jquery you can trigger a submit. Check this : https://api.jquery.com/submit/

You create a div or other and in jquery you add a listener to this div :

<div id="button">Here my button</div>

And in jquery

$('#button').on('click', function(){
    $('#update-form').submit()
}
Nightmare answered 26/9, 2016 at 12:22 Comment(2)
I used it in my code above, but i have multiple submit buttons. My requirement is I want to set different target for both buttons. (I recommend you to read my question once again)Forrester
Add a click listener on both buttons who do different things no ?Nightmare
F
1
<form id="update-form">
    ...
    ...
    <button type="button" class="submitForm" formaction="search.php">Search</button>
    <button type="button" class="submitForm" formaction="update.php">Update</button>
</form>

And then your JS:

$('.submitForm').click(function(){
    $.ajax({
        method: 'post',
        url: $(this).attr('formaction),
        data: $("#update-form").serialize(),
        ...
        ...
        success: function(){  }
    });

});
Frenzy answered 26/9, 2016 at 13:15 Comment(1)
This approach uses the standard "formaction" attribute of a submit button, which makes it useful when converting an existing plain-HTML form with many submit buttons.Meadowlark
K
0

   $(document).ready(function(){  
           $(document).on('submit', '#update-form', function(){
             
                var data = $(this).serialize();
                $.ajax({
                    type : 'POST',
                    url  : 'search.php',
                    data : data,
                    success :  function(data){
                        $(".display").html(data);
                    }
                });
                return false;
            });
     
     $(document).on('click', '#update-btn', function(){
                var data = $("input[name='filenum']").val(); //Handle how you want
       
                $.ajax({
                    type : 'POST',
                    url  : 'update.php',
                    data : data,
                    success :  function(data){
                        $(".display").html(data);
                    }
                });
              });
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div id="update" class="tab-pane fade">
                <form id="update-form" role="form" class="container" method="post" action="search.php">
                    <h3>Update details</h3>
                    <table class="display">
                        <tr  class="space">
                            <td><label>File Number:</label></td>
                            <td><input type="text" class="form-control" name="filenum" required></td>
                        </tr>
                    </table>
                    <button type="submit" class="btn btn-default text-center" name="search_btn" >Search</button>      
                    <button form='forn-is-not-exist' class="btn btn-default text-center"  name="submit_btn" id="update-btn" >Update</button>
                </form>
            </div>
</body>

Check it out

Kobylak answered 26/9, 2016 at 12:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.