PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 8, 2022

[FIXED] How to use AJAX for different functions when more buttons included in the form

 September 08, 2022     ajax, jquery     No comments   

Issue

I have a model as below in PHP.

<div id="Modal" class="modal fade">
 <div class="modal-dialog">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title"  >Block</h4>
   </div>
   <div class="modal-body">
    <form method="post" id="block_form">
     <label>Number</label>
     <input type="text" name="num_block" id="num_block" class="form-control" readonly/>
     <br />
     <label>Updated By</label>
     <input name="blockedBy" id="blockedBy" class="form-control" readonly/>
     <br />

    
     <input type="hidden" name="employee_id_update" id="employee_id_update" />
     <input  type="submit" name="block1" id="block1" value="Block 1" class="btn btn-success" />
      <input  type="submit" name="block2" id="block2" value="Block 2" class="btn btn-success" />
    </form>
   </div>
   <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal" >Close</button>
   </div>
  </div>
 </div>
</div>

I need to click two different buttons; block1 and block2 and do two different functions after submitting the form. Here is my try

 $('#block_form').on("button1", function(event){  
           event.preventDefault();
            
           alert("clicked Btn1");
            
    });

This is not working and page refreshes only. But when I replace $('#block_form').on("submit", function(event){ I can see it works but I can see the same alert for both buttons since the type submit is same for both buttons. Can someone show me how to do this?


Solution

https://api.jquery.com/on/

// can change ".btn-success" by "#block1, #block2"
$('#block_form').on("click", ".btn-success", function(event){  
   event.preventDefault();
   
   var getBtnID = $(this).attr('id');
   
   alert("clicked " + getBtnID);
      
   if (getBtnID == "block1") {
      // do something
   }
   
   if (getBtnID == "block2") {
      // do something else
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="Modal" class="modal fade">
 <div class="modal-dialog">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title"  >Block</h4>
   </div>
   <div class="modal-body">
    <form method="post" id="block_form">
     <label>Number</label>
     <input type="text" name="num_block" id="num_block" class="form-control" readonly/>
     <br />
     <label>Updated By</label>
     <input name="blockedBy" id="blockedBy" class="form-control" readonly/>
     <br />

    
     <input type="hidden" name="employee_id_update" id="employee_id_update" />
     <input  type="submit" name="block1" id="block1" value="Block 1" class="btn btn-success" />
      <input  type="submit" name="block2" id="block2" value="Block 2" class="btn btn-success" />
    </form>
   </div>
   <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal" >Close</button>
   </div>
  </div>
 </div>
</div>



Answered By - SKJ
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing