-2
The simple and practical method I found here in Stack of passing a PHP variable to a JS has worked well so far. However, with a certain content it only works if I feed this PHP variable, but let this variable receive this content through a query, no. (OBS: The full query set result has 13,700 characters)
<?PHP
$consulta = mysql_query("SELECT MODELO FROM celular_banco");
while($resultado=mysql_fetch_array($consulta))
{
$fracasso .= $resultado["MODELO"].',';
}
echo $fracasso; // LGA270.AVIVBK,LGA270.ACLRBK,
$sucesso = "LGA270.AVIVBK,LGA270.ACLRBK,";
?>
<script>
var duvida = '<?PHP $sucesso;?>';
document.write(duvida); // OK!
</script>
FUNCIONA
<script>
var duvida = '<?PHP $fracasso;?>';
document.write(duvida); // SEM RESULTADO!
</script>
Não FUNCIONA
Try this on
PHP
:echo (string) $fracasso;
– Thiago Magalhães
The variables doubt in the two scripts will be var doubt = '; Does it have to be <?PHP echo $success;? > and <?PHP echo $failure;? > Or
<?= $sucesso;?>
and<?= $fracasso;?>
– user60252
We need echo otherwise the JS does not understand. Or I did not understand your help...
– Anderson Brandão
Comrade. Unfortunately it didn’t work with the (string)
– Anderson Brandão
Why exactly do you need to pass this content to JS for it to write in the document? It just doesn’t make sense and, if you saw something like this here on the site, it would be nice to inform, because there’s something very wrong there.
– Woss
Anderson, Come on: First I took the autocomplete script in W3schools. In this script the JS has an array with the predefined items (countries). Here I need to put a content from a php query. That’s why I’m trying to move the PHP variable to JS. I’ve done it several times with Barcode and others but now I don’t know why the hell it doesn’t work... I can even pass other values of a simple query. But when I try to pass the result of this While does not work. I am very suspicious that the problem is this While. But I do not know what is blocking.
– Anderson Brandão
What I saw here in Stack and I’ve done a lot and worked very well so far was the PHP>JS conversion scheme. I posted the code on the question itself (The $success variable)
– Anderson Brandão