How to pass values to a PHP on the same page and keep them selected

0

In this case I am sending values (of a select and a number), the values pass normally, but the problem is that when I select one, the other one is deselected, but what I would like is that they be selected (if is that you select the two), as you could do so that when you select a value, it is marked in your select.

  <!DOCTYPE html>
  <html>
 <head>
 <title></title>
 </head>
 <body>
 <h2>Pasar Valores</h2>
 <form action="buscar.php">
    <select name="miselector" id="miselector" onchange="this.form.submit()">
        <option value="">seleccionar</option>
        <option value="coches">coches</option>
        <option value="casas">casas</option>
    </select>

  <input type="Number" name="semana" id="semana" style="width: 50px; height: 25px; text-align: center;position: center" step="1">
  </form>

   <?php
     if (isset($_GET['miselector']) or isset($_GET['semana'])) {
       echo "<br>estado: ".$_GET["miselector"];
       echo "<br>semana: ".$_GET["semana"];
     }
   ?>
</body>
 </html>
    
asked by kev 18.06.2018 в 22:16
source

2 answers

0

You can add JS to solve this case, for example the following case

 <!DOCTYPE html>
  <html>
 <head>
 <title></title>
 </head>
 <body>
 <h2>Pasar Valores</h2>
 <form action="index.php">
    <select name="miselector" id="miselector" onchange="this.form.submit()">
        <option value="">seleccionar</option>
        <option value="coches">coches</option>
        <option value="casas">casas</option>
    </select>

  <input type="Number" name="semana" id="semana" style="width: 50px; height: 25px; text-align: center;position: center" step="1">
  </form>

   <?php
     if (isset($_GET['miselector']) or isset($_GET['semana'])) {
       echo "<br>estado: ".$_GET["miselector"];
       echo "<br>semana: ".$_GET["semana"];
     }
   ?>
</body>

<script>
document.getElementById("miselector").value = "<?php echo($_GET["miselector"]);?>";
document.getElementById("semana").value = "<?php echo($_GET["semana"]);?>";
</script>

 </html>
    
answered by 19.06.2018 / 01:16
source
2

In that case, you should use a select multiple , and here you would need a button to submit the form (the selected values) to be able to read them from php, since otherwise, the onchange event will be executed as soon as you select the first element of your select.

This example is very clear.

    
answered by 18.06.2018 в 22:25