1
I’m trying to catch the id
of a product through a jQuery function, but I need to pass this variable to php, and as this chunk of PHP code is already inside the <script></script>
does not work the way I did below, so I would like to know if there is a way to get this value directly in the PHP snippet, or if there is another way to get this value.
Remembering that I get the value only after selecting an option.
<select name="txt_os" id="txt_os" class="so">
<option value="-1">Selecione uma opção</option>
<?php
while($linha = self::listar($qry))
{
echo "<option value=$linha[id]> $linha[titulo]</option>\n";
}
?>
</select>
<script>
var id = [];
$(document).ready(function(){
$(".so").change(function(){
if (parseFloat($(this).val()) != -1)
{
id[0] = parseInt($(this).val());
alert(id[0]);
"<?php
$idG = "<script>document.write(id[0])</script>";
$sqlG = "SELECT * FROM gabinete WHERE id='$idG'";
$g-> verDados($sqlG, 0);
$precoG = $g->getPreco();
?>"
precoG = "<?php echo $precoG?>";
alert(precoG);
}
})
})
</script>
Exchanging
$idG = "<script>document.write(id[0])</script>";
for
$idG = "1";
It works normally, but I would determine the outcome myself, which is not the idea.
Shows the id that is in the option selected in the first, in the second the result is empty.
– Vinícius
But what second?
– Bruno Augusto
@Brunoaugusto has two Alert in the code, Alert(id[0]); and Alert(Precog);
– Vinícius
The problem is this Document.write(). Since PHP is embedded in Javascript, after the equal sign of $idG you close double quotes, concatenate with the plus sign (+) put the normal JS variable, concatenate again and reopen double quotes
– Bruno Augusto
@This would look like this: "<? php $idG = " + id[0] + "; $sqlG .... ? If it didn’t work.
– Vinícius