how to pass scrip variables that interpret a sql statement?

1

Good is the first time that I participate I am wanting to make the variables: xco, xto, xde, xcc recognize me in the query sql q I do below in vsql, it will not be possible if you can give me a hand or some idea to be able to find that way, thanks in advance

function mostrarValores()
            {


                xco = parseFloat(document.getElementById('idcondominio').value);
                xto = parseFloat(document.getElementById('idtorre').value);
                xde = parseFloat(document.getElementById('iddepartamento').value);
                xcc = parseFloat(document.getElementById('idconcepto_consumo').value);


                var vsql = '<?php echo $sql=DB::table('costo_condominio_mensual')->select('Pago')->where('idcondominio','=',  xco)->where('idtorre','=',xto)->where('iddepartamento','=', xde )->where('idconcepto_consumo','=', xcc )->get(); ?>'
                $("#consumo").val(vsql)

            }
    
asked by JBQ 15.07.2018 в 20:53
source

3 answers

0

First knight, the document in PHP does not EXIST !!!

There is the client side and the server side .. You can not mix 2 things at once. When you write JavaScript code it is the browser that interprets it, a verity the PHP processor has processed it, that is, you try to introduce JavaScript variables that have not yet been processed in PHP code. Come on, you can not ... A greeting. I recommend you see some videos. link

    
answered by 15.07.2018 в 21:39
0

Look you can call variables from JS to PHP and vice versa, I'll show you an example:

From PHP to JS

<?php $variablePHP = “contenido de la variable php”; ?>
<script>
var variableJS = “<?php echo $variablePHP; ?>” ;
document.write(“VariableJS = ” + variableJS);
</script>

From JS to PHP

<script> var variableJS = “contenido de la variable javascript”; </script>
<?php
$variablePHP = “<script> document.write(variableJS) </script>”;
echo “variablePHP = “.$variablePHP;
?>

You just have to make sure you send the JS and PHP files correctly (the address where they are)

And once the variables are obtained you can use them in your SQL statement

    
answered by 15.07.2018 в 22:01
0

What happens is that when the document is loaded the php has already been uploaded to the server, (php is loaded on the server side and js on the client side), so when it comes to the function the php has already been loaded and the SQL will not take effect, therefore it will not bring anything, I had the same problem and solve it with ajax ... it would be something like this: create a php file with name my_new.php, in which everything will go php and at the end you give an echo ..

xco = $_POS['xco'];
xto = $_POS['xto'];
xde = $_POS['xde'];
xcc = $_POS['xcc'];

$sql=DB::table('costo_condominio_mensual')->select('Pago')->where('idcondominio','=',  xco)->where('idtorre','=',xto)->where('iddepartamento','=', xde )->where('idconcepto_consumo','=', xcc )->get();

echo $sql;

and that would be your initial document

function mostrarValores()
        {
            xco = parseFloat(document.getElementById('idcondominio').value);
            xto = parseFloat(document.getElementById('idtorre').value);
            xde = parseFloat(document.getElementById('iddepartamento').value);
            xcc = parseFloat(document.getElementById('idconcepto_consumo').value);

var parametros = {'xco' : xco, 'xto' : xto, 'xde' : xde, 'xcc' : xcc}

$.ajax({
            //data:  parametros,
            url:   "mi_nuevo.php",
            type:  "post",
            error : function(xhr, status) {
                alert('Disculpe, existió un problema');
            },
            success: function(response){
                $("#consumo").val(response)

            },
        }); 



        }

I hope it helps you, and the marks as it turns out, By: JJ

    
answered by 15.07.2018 в 22:02