How to use the option value in php?

0

I have a drop-down menu, with 2 options:

New customer and New product.

What I'm looking for is that having selected a certain option takes me to a different form, in case I select "New Product" that takes me to a form where I add this information.

The problem is that I do not remember the code with which this was done ...

Menu code:

<form name="form1" method="post" onSubmit="return validar();">
  <p>
    <label for="select"></label>
    <select name="menu" id="menu">
      <option value="nc">Nuevo Cliente</option>
      <option value="np">Nuevo Producto</option>
    </select>
     <input type ="submit" value ="Registrar" />
</form>

Valid which option is selected with javascript, but I do not remember how to start another form depending on the selected option.

<script>
  function validar(valor) {
    if (menu.value == "nc") {
      // Quiero saber el código para cambiar de formulario
      // en caso de que seleccione esta opción
    }   
    if (menu.value == "np") {
      // Quiero saber el código para cambiar de formulario
      // en caso de que seleccione esta opción
    }
  }
</script>
    
asked by Alejandro Esquivel 16.04.2018 в 02:01
source

3 answers

-1
<script>
 function validar(valor) {
   if (menu.value == "nc") {
    location.href = "nuevoproducto.php";
   } else if (menu.value == "np") {  
    location.href = "nuevocliente.php";
   }
}
</script>

<form name="form1" method="post" onSubmit="return validar(this);">
  <p>
    <label for="select"></label>
    <select name="menu" id="menu">
      <option value="nc">Nuevo Cliente</option>
      <option value="np">Nuevo Producto</option>
    </select>
    <input type ="submit" value ="Registrar" />
</form>

This way you can do it, if what you want is that according to the option send to one form or another.

    
answered by 16.04.2018 / 02:59
source
0

Try assigning the event onchange to select , sending the reference of the select with this . Then you verify what value was selected using the value property:

function redireccionar(select){
  if(select.value == "nc") {
    console.log("redireccionar a nc");
  } else if(select.value == "np"){
    console.log("redireccionar a np");
  } else{
    console.log("ninguna opcion valida seleccionada");
  }
}
<form name="form1" method="post" onSubmit="return validar();">
  <p>
    <label for="select"></label>
    <select name="menu" id="menu" onchange="redireccionar(this)">
      <option value="nc">Nuevo Cliente</option>
      <option value="np">Nuevo Producto</option>
    </select>
     <input type ="submit" value ="Registrar" />
</form>
    
answered by 16.04.2018 в 03:06
0

It can be done in different ways, I'll give you one using jQuery :

$(document).ready(function() {
  // capturo el evento cada vez que cambia el selector
  $('#menu').on('change', function() {
    // obtengo el valor
    var value = $(this).val();
    
    // si es np cambio el action del form por nuevoproducto
    if (value == 'np') {
      $('#form1').attr('action', 'nuevoproducto.php');
    }
    // si es nc cambio el action del form por nuevocliente
    else if (value == 'nc') {
      $('#form1').attr('action', 'nuevocliente.php');
    }
  });
});
<form id="form1" name="form1" method="post">
  <p>
    <label for="select"></label>
    <select name="menu" id="menu">
      <option value="nc">Nuevo Cliente</option>
      <option value="np">Nuevo Producto</option>
    </select>
     <input type ="submit" value ="Registrar" />
</form>

Then each time you change the combo, you will send the form to one file or another depending on what you select.

    
answered by 16.04.2018 в 03:13