call a javascript function in php (to then make a query in the php)

-2
  

Top: previous consultation of the data

<?php
  function conexion(){
    $conn = null;
    $host = 'localhost';
    $db = 'mi_base_datos';
    $user = 'usuario';
    $pwd = '';

    try{
      $conn = new PDO('mysql:host='.$host.'; dbname='.$db, $user, $pwd);
    }catch(PDOException $e){
      exit();
    }
    return $conn;
  }

  $cn = conexion();
  $sql= "select idMateriaPrima as id, nombre as name from materiaprima where estado = 1";
  $query = $cn->query($sql);
  $datos = $query->fetchAll();

?>
  

Middle part: use of calculated data, embedded as JSON in the HTML

var variableJS = document.getElementById('datoHaObtener').value;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div ng-init='friends = <?php echo json_encode($datos); ?>'></div>
  <div class="form-group">
    <label for="banco" class="col-sm-3 control-label">Producto</label>
    <div class="col-sm-5">
      <input id="datoHaObtener" list="searchTextResults"  class="form-control" ng-model="searchText" required>
      <datalist id="searchTextResults">
        <option  ng-repeat="friend in friends | filter:searchText" value="{{friend.name}}"></option>
      </datalist>
      
      <br>
      
      <div class="form-group">
      <label for="banco" class="col-sm-3 control-label">Precio</label>
      <select required class="form-control" name="precixd" id="precixd">
        <option  value="0">Seleccione</option>
      </select>
      <label for="banco" class="col-sm-3 control-label">Precio</label>
      <input  id="helloo" type="text" name="">
    </div>
  </div>
</div>
  

Final part: What I want to do

<?php

    $ConsultaDato = "(aqui quiero el dato que me debe pasar el javascript)";
    $conecta = mysqli_connect("localhost", "root", "", "sistemahierroforjado");

    $resultado = mysqli_query($conecta, "SELECT precioUnitario FROM materiaprima where nombre = '$ConsultaDato' ");
    while ($consulta = mysqli_fetch_array($resultado))
    {
        $respuesta = $consulta['precioUnitario'];
    }
    echo "$respuesta";
 ?>
    
asked by alvin 06.07.2017 в 18:28
source

1 answer

0

Try something like this:

<script>
  var num =document.getElementById('ObtieneDatoImput').value;

  jQuery.get("prueba.php", {numero:num})
    .then(function (response) {
      document.getElementById("resultado").value = response;
    }
  );
</script>

With jQuery.get the request is made to the server, to the file called test.php, and the data {number: num} is passed to it, that in php it will receive an array, with key number, and value of the number that you had obtained. (You will have to have active jQuery for this case)

And then the result of the file is what will arrive at the response of the function js.

// me obtine lo del impul que esta seleccionado
<?php
    $jxj = $_GET['numero'];
    $conecta = mysqli_connect("localhost", "root", "", "sistemahierroforjado");

    $resultado = mysqli_query($conecta, "SELECT precioUnitario FROM materiaprima where nombre = '$jxj' ");
      while ($consulta = mysqli_fetch_array($resultado))
      {
        $respuesta = $consulta['precioUnitario'];
      }
      echo $respuesta;
?>

<script>

I hope you serve, greetings.

    
answered by 06.07.2017 в 18:55