Passing PHP variable in AJAX

0

I have a script.js file that needs to capture a variable in php, how do I?

See what I have:

function atualizaFotos() {

    $.ajax({
        url: "imovel-fotos-id.php?cliente=&cod="
    }).done(function (resposta) {
        $("#fotos").html(resposta);
    });

}

I have the client variable and the code variable. How to capture a php variable and pass it on this parameter?

I look forward to helping you!

Thank you!

    
asked by anonymous 22.02.2016 / 13:02

1 answer

0

You can put your php variable in input type hidden , and send via parameters to the js file. For example:

<input type="hidden" id="cliente" value="<?php echo $var_cliente_php; ?>">
<input type="hidden" id="codigo" value="<?php echo $var_codigo_php; ?>">
<input type="button" onclick="atualizaFotos(document.getElementById("cliente").value, document.getElementById("codigo").value)" value="Enviar">

and your jQuery function looks like this:

function atualizaFotos(pCliente, pCodigo) {
  $.ajax({
    url: "imovel-fotos-id.php?cliente="+pCliente+"&cod="+pCodigo
  }).done(function (resposta) {
    $("#fotos").html(resposta);
  });
}
    
22.02.2016 / 13:27