Change mysql query with javascript

0

I have a problem and I do not know how to solve it, I show my code of a select loaded with mysql data,

     <?php 
  $link = mysqli_connect("localhost", "root", "");
  mysqli_select_db($link, "stp17");
  $tildes = $link->query("SET NAMES 'utf8'"); //Para que se muestren las tildes correctamente


  $resulta = mysqli_query($link, "SELECT * FROM estados where nombre='$nombreDelegacion'");

    while ($fila = mysqli_fetch_array($resulta)){

      $claveEstado=$fila['clave'];

    }

  $resultas = mysqli_query($link, "SELECT * FROM rutas WHERE (cveorig='$claveEstado' OR cvedest='$claveEstado') AND cliente='CLI140002'");?>

  <div class="input-group">
     <span class="input-group-addon">Ruta: </span>
      <select name="rutas" id="estados" value="0" class="form-control" onchange="showUser(this.value)">
        <?php
          while ($fil = mysqli_fetch_array($resultas)){  ?>

            <option value="<?php echo $fil['id']; ?>"><?php echo $fil['descripcion']; ?></option>

          <?php    } ?>
        </select>
      <span class="input-group-btn">
      <a href="rutas/nuevaRuta.php" class="btn btn-success" role="button"><span class="glyphicon glyphicon-plus-sign"></span></a>
  </span>
  </div>

What I want to do is change the query that I have it so

$resultas = mysqli_query($link, "SELECT * FROM rutas WHERE (cveorig='$claveEstado' OR cvedest='$claveEstado') AND cliente='CLI140002'");?>

To a query like this:

$resultas = mysqli_query($link, "SELECT * FROM rutas");

All this clicking on a checkbox, when I have selected it, it shows me the second query and when I do not have it selected it shows me the first query

    
asked by Jose Luis GP 28.03.2017 в 17:22
source

1 answer

0

If you use the option to reload your page you can make the form redirect itself and use the POST method, and then recover the values in PHP .

In the following code I concatenate the string in case the checkbox is selected; for your case it would be the script.

<?php
$cadena = "Hola";
$selecionado = "";

if(!empty($_POST) && isset($_POST["control"])){ 
    $cadena .= " Mundo";    
    $selecionado = "checked";
}
?>

<html>
<head>
</head>
<body>
<form id="idFormulario" method="POST">
    <h5><?php echo $cadena; ?></h5>

    <input id="control" name="control" type="checkbox" <?php echo $selecionado; ?>>     
</form>
</body>
<script type="text/javascript">
    var form = document.getElementById('idFormulario');

    document.getElementById('control').addEventListener('change', function () {                 
        form.submit();
    });
</script>
</html>

Example:

As you read in the comments, you can use AJAX that will not allow a reload of the page.

    
answered by 28.03.2017 в 19:40