Pass JavaScript variable to PHP? [closed]

4

By means of a combobox (loaded by names of a database using a while I made a function of javascript that when I choose a name of combobox I bring in a separate input the card of that name .

I have read about js and it was clear to me that js is only from the client, as such it can not interact from the server.

Could you help with the steps of converting a variable js to php to insert it into a database?

    
asked by Rene Limon 22.12.2016 в 17:46
source

4 answers

5

This is your Combobox, which as you mention you have no problem filling

  

My.html

<select data-no-selected="Sin Seleccionar"  id="tuid"  name="tuid"  required class="form-control></select>

In a JS file, in this part when the file loads, what you put in your Load will be executed, example:

  

Here you can fill in your Combobox

     

Mi.JS

$("#micombo").load("llenarcombo.php");

But what does llenacombo.php contain?

 $orden=mysqli_query($cn,"select * from tutabla");
 echo '<option value="">Seleccione el Registro</option>';
 while($datos=mysqli_fetch_array($orden))
 {
    echo '<option value="'.$datos['id'].'">'.$datos['nombre'].' </option>';
 }
  

As you mention it when you change the selection of your combo, you should send the variable this is thanks to the "Change" event, this variable is collected in your php

$("document").ready(function()
{
 $("#tuid").change(function(){
  var id2=$("#tuid").val();
  $.get("tuphp.php",{parametro:id2})
  .done(function(data){
  $("#mostraren").html(data);
  })
 })
}

But what does your php.php contain?

$param=$_GET["parametro"];
include('conexion.php');
$orden=mysqli_query($cn,"select * from tabla2 where id=$param");
echo '<option value="">Seleccione Cedula</option>';
while($datos=mysqli_fetch_array($orden))
{
    echo '<option value="'.$datos['id'].'">'.$datos['nombre'].'</option>';
}

Y To insert Data?

In this part you receive the parameters that you sent through JS

 $param=$_GET["parametro"];
 $param1=$_GET["parametro1"];
 $orden=mysqli_query($cn,"INSERT INTO tabla2 (dato1,dato2) values param=$param1 where id=$param");
 echo "Datos Insertado";
    
answered by 22.12.2016 в 18:02
3

This would be the Javascript part, where we assign the value to the variable to the function

<script type="text/javascript">
    var variable ="Hola esta es mi variable";

    function onEnviar(){
       document.getElementById("variable").value=variable;
    }
</script>

And this is the form where we are going to give the value to the "variable", when we click on the button we invoke the onsubmit method that will call our function JavaScript

<form action="ejemploVariable.php" id="formulario" method="post" name="formulario" onsubmit="onAceptar()">
    <input id="variable" name="variable" type="hidden" />
    <input id="aceptar" type="submit" value="Aceptar" />
</form>

exampleVariable.php

We read the parameters that were passed through POST, in this case "variable"

<?php
    echo "Resultado variable: ".$_POST['variable'];
?>

And with this we would see the following on the screen:

Variable result: Hi this is my variable

    
answered by 22.12.2016 в 19:04
1

This you could do with ajax . As I see that you are using JQuery , you could easily do it in the following way:

$.ajax({
    type: 'POST',
    url: tuUrl,
    data: {tuVariable: 'tuVariable'},
    success: exitoso
})

In this way, you can call the PHP file that you need to use using the url attribute.

With the attribute data you can send the variable of Javascript that you have to use in your PHP and with the attribute success you will be calling the function exitoso when you have performed your PHP functions, that is, when the ajax function has been completed.

In PHP you could simply recover the data by $_POST['tuVariable'] .

    
answered by 22.12.2016 в 17:52
0

Right! jquery is a framework that works on the client side, but IF it can interact with server data, there are different ways to obtain and send data to a file php . One of the busiest is ajax that helps you do asynchronous tasks (in the background).

I imagine that your file where you have your data sends you an array or queries to the database suppose you have a variable called $datos , this variable contains an array with names and their respective cedulas. To work with jquery that data you can do:

var datos = '<?php echo json_encode($datos); ?>';

With this code you will get a json fix of your data then you can play with

//es la variable anterior guardado
var nombres = JSON.parse(datos);

And here you can go through your data as a jquery arrangement. The rest is to append the data with ids

    
answered by 23.12.2016 в 00:07