How to create several buttons in the same HTML / PHP form?

0

I am starting the learning of forms in HTML and PHP, and I managed to create a button that sends the entered data to a MySQL database entered by the user.

My question is, how can I put more buttons in the same form and that each one perform a different action? I ask this because from what I have understood the part of "action" within the code of the form is the action that takes place when you click on the button.

Example:

<div><center>
<form method="post" action="InsertarSQL.php" class="forma">

Complete form code

  <div><center>
  <form method="post" action="InsertarSQL.php" class="forma">
  <input type="Text" name="apellido_pat" placeholder="Apellido Paterno"><br> 
  <br>
  <input type="Text" name="apellido_mat" placeholder="Apellido Materno"><br> 
  <br>
  <input type="Text" name="nombre" placeholder="Nombre"><br><br>
  <input type="Text" name="puesto" placeholder="Puesto"><br><br>
  <input type="Text" name="edad" placeholder = "Edad"><br><br>
  <input type="Submit" name="enviar" value="Agregar registro">
  </form>
  </center></div>
  </div></center>

  <a href="Principal.html" class="init">Inicio</a>
  </body>
    
asked by Erick Finn 20.09.2018 в 16:21
source

2 answers

2

<form id='form1' name='form1'>
    <input type="button" value="Crear usuario" id="nuevo" name="nuevo" onclick= "document.form1.action = 'nuevo.php'; 
    document.form1.submit()" />

    <input type="button" value="Eliminar usuario" id="eliminar"
    name="eliminar" onclick= "document.form1.action = 'eliminar.php'; document.form1.submit()" />
</form>

or you can Capture the button pressed on the controller

<form action="/TransaccionSQL.php" name="form1" id="form1" method="post">
<input type="submit" value="Crear usuario" id="evento_nuevo"    name="evento_nuevo" /> 
<input type="submit" value="Eliminar usuario" id="evento_eliminar" name="evento_eliminar" />
</form>
$add=$_POST["evento_nuevo"];
$del=$_POST["evento_eliminar"];
if($add!=null){
  /*####*/
}
if($del!=null){
  /*####*/
}

Reference link

    
answered by 20.09.2018 / 17:03
source
1

You can put a button of type="submit" and the other buttons with the type="button"

         

Another alternative is to add the event onsubmit="return 0;" in the form and add independent events for each button with either the onclick or another that fits your need.

    
answered by 20.09.2018 в 17:20