I need a little help with the following code:
I have a simple form composed of 1 combobox(Select) and several textbox(inputs)
What I need is for the user to fill in all the fields or not, send the values through two buttons, of course, each button is a different .php page that will receive the same parameters. But each one is programmed to do something different.
As method of sending parameters by form that I use is GET
I attach my HTML code
<body>
<form action="" id="form" method="GET" >
<select id="controlselect" name="ordenar">
<option value='0'>Seleccione</option>
<option value='1'>General</option>
<option value='2'>Producto</option>
</select>
<input type='text' name='txtFecha' size='6' maxlength='20' id='txtfecha'>
<input type='text' name='txtFechaFinal' size='6' maxlength='20' id='txtfechafinal'>
<input type='text' name='txtIdentificacion' size='6' maxlength='20' id='identificacion'>
<a href="" id="Buscar"><button type="button">Buscar</button></a>
<a href="" id="Generar"><button type="button">Generar</button></a>
</form>
</body>
I have managed to make it send only the combobox , but when trying to adapt my code with the textbox , I have encountered errors.
I am going to show you my adaptation with a single Textbox , which is generating the errors
Attached my JavaScript Code
<script>
let btnBuscar = document.getElementById('Buscar');
let btnGenerar = document.getElementById('Generar');
$("#controlselect").change(function()
{
valor = $("#controlselect").value();
});
$("#Buscar").click(function(){
valor1 = $("#txtfecha").val();
});
btnBuscar.setAttribute('href',`Prueba8.php?valor=${valor}`+`${valor1}`);
btnGenerar.setAttribute('href',`Prueba9.php?valor=${valor}`+`${valor1}`);
</script>
I attach my PHP Code for testing
<?php
$v1 = $_GET['valor'];
$v2 = $_GET['valor1'];
echo("Variable: $v1");
echo("Variable: $v2");
?>
I would like you to tell me the problem and if you can give me a solution adapted to my code or if necessary, a new one.
Thanks in advance!
There are several errors:
valor = $("#controlselect").value();
if you're going to usejQuery
it like this you don't get the value, it's with.val()
.Prueba8.php?valor=${valor}
+${valor1}
, this is not the way to concatenate, when you use template strings it is no longer necessary to use the+
to concatenate, also to pass data by URL if you have noticed when you enter a page that uses filters this appearswww.lapagina.com/busqueda.php?id=dfg543&longitud=23434...
thatAmpersand
works to add another value to the URL, then we do it as followsPrueba9.php?selectVal=${selectVal}&date=${date}&fFinal=${fFinal}&id=${id}
.This time we remove the
change
one from the select since you are going to send the values when you press any button and we are going to send it with awindow.location.href
, I hope it works for you, let me know if you have any questions.IN PHP