How to pass the value of a SELECT to the same page

1

I am passing variables of a select; through onchange, but the variables are passed from one page to another. this is my html and I'm on the page ejm.php:

<body>
<select name="miselector" id="miselector" onchange="enviar_valores(this.value);">
   <option value="">Seleccionar</option>
   <option value="coches">Coches</option>
   <option value="casas">Casas</option>
</select>

<script>

    function enviar_valores(valor){
    //Pasa los parámetros a la pagina buscar
    location.href='buscar.php?valor=' + valor;
     }

</script>
</body>

then here take the variable and pass it to the search.php page:

<?php 


   $valor=$_GET['valor'];

   echo $valor;

 ?>

but what I'm looking for is that the variables are passed on the same page, can that be done?

    
asked by kev 18.06.2018 в 19:27
source

2 answers

1

In this case, to access the value of the variable from php (it is executed on the server) it is necessary to make a submit of the form where you capture that value. For example:

file "a.php":

<!DOCTYPE html>
<html>
<body>

<h2>HTML Forms</h2>

<form action="a.php">
 <select name="miselector" id="miselector" onchange="this.form.submit()">
 <option value="">Seleccionar</option>
 <option value="coches">Coches</option>
 <option value="casas">Casas</option>
</form>

///Lectura de la variable "en la misma página" desde php

<?php
echo "Select: ".$_GET["miselector"];
?>
</body>
</html>

In any case, this way of handling the data is not the most appropriate. I recommend structuring your code better, to separate the executions and thus have a better control of the flow of your application.

    
answered by 18.06.2018 / 21:14
source
1

Replace search.php with ejm.php

location.href='ejm.php?valor=' + valor;
    
answered by 18.06.2018 в 21:50