How to run form action and onclick event at the same time?

Viewed 507

In this scenario only the Javascript function works. But I want to run JavaScript function(onclick event) and form action (my PHP backend) at the same time, how to do that?

<tr id="tr4" class="table-danger">
  <td>Geethan</td>
  <td>
    <form action='include/action.php?user_id=3' method='post'>
      <input id="tr4_btn" class="btn btn-warning" type="button" value="Take action" onclick="takeaction(this.id,'tr4','tr4_i')" />
      <i id="tr4_i" class="fa " style="color:green;"></i>
    </form>
  </td>
</tr>

1 Answers

Because when you submit the form, the website will be redirect to form action, so your action if not completed, it will be cancelled. You can try this way (jQuery).

function takeaction(id,$val1,$val2){
     var $this = jQuery('#'+id);
     var $form = $this.parents('form');
     //...your javascript code at here
     // after run your code
    $form.submit();// to submit form
}
Related