Change the action value inside the form

Asked

Viewed 626 times

-3

I have a code with a "form" and I have two buttons that I want you to direct to different pages. By clicking the finish button I would like the "action" to be action='envio1.php' and by clicking the Change button the "action" to be action='envio2.php'

Note: I know it is a very simple problem, however, I know that this problem will be solved with javascript and I do not understand much.

<form method='post' action='envio1.php' enctype='multipart/form-data'>
  <div class='row'>
    <div class='col-sm-5'>
      <h4 class='modal-title' style='margin-right: 140px;'></h4>
    </div>
    <div class='col-sm-5 text-right' style="text-decoration:none; color:white; float: right">
      <button class='btn btn-danger' name='FINALIZAR' value="OK">FINALIZAR</button>
      <button type='submit' class='btn btn-success'>ALTERAR</button>
    </div>
  </div>
</form>

  • 1

    What is value A and value B?

  • I’ll rephrase the question

  • It’s good because you’re a candidate for closure.

1 answer

1


You can change the action of form in the onClick of each button:

HTML:

<form id='form' method='post' enctype='multipart/form-data'>
    <button type='submit' id='botaoFinalizar' class='btn btn-danger' name='FINALIZAR' value="OK">FINALIZAR</button>
    <button type='submit' id='botaoEnviar' class='btn btn-success'>ALTERAR</button>
</form>

JS:

document.getElementById('botaoFinalizar').onclick = function() {
    document.getElementById('form').action = '/envio1.php';
}
document.getElementById('botaoEnviar').onclick = function() {
    document.getElementById('form').action = '/envio2.php';
}

If the form is not submitted, you can use the .submit() in the form after changing the .action.


Or you can use the attribute formaction. See the Can I use to understand which devices can interpret this attribute.

In accordance with MDN:

formaction: The URI of a program that processes the submitted information by the button. If specified, it overrides the action attribute of button owning form.

PS: The acceptance of this attribute in button is significantly higher than if you use <input type='submit'>, in accordance with this link.

<button type='submit' class='btn btn-danger' name='FINALIZAR' value="OK" formaction='/envio1.php'>FINALIZAR</button>
<button type='submit' class='btn btn-success' formaction='/envio2.php'>ALTERAR</button>
  • Excellent based on your reply I did here and it worked. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.