PHP variable in Javascript

Asked

Viewed 92 times

-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;

  • 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;?>

  • We need echo otherwise the JS does not understand. Or I did not understand your help...

  • Comrade. Unfortunately it didn’t work with the (string)

  • 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.

  • 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.

  • 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)

Show 2 more comments

3 answers

2


If your PHP delivers its own HTML to the client, Javascript doesn’t even need to go into history. What you’ve tried to do, even if you’ve seen it on W3scholls or right here on the site, is gambiarra.

What you can do is deliver the ready HTML. Something like:

<input list="modelos" />
<datalist id="modelos">
<?php
  $consulta = mysql_query("SELECT MODELO FROM celular_banco");

  while($resultado = mysql_fetch_array($consulta))
  {
    echo "<option value='{$resultado['MODELO']}'/>";
  }
?>
</datalist>

This will generate for the client an HTML of the type:

<input list="modelos" />
<datalist id="modelos">
  <option value="Foo"/>
  <option value="Bar"/>
  <option value="Baz"/>
  <option value="Acme"/>
  <option value="Zing"/>
</datalist>

That has the suggestion property as the user type.

What is the </datalist tag for>?

Another option you have is to feed your suggestion in the form asynchronously, via AJAX request, but if you already deliver HTML ready with PHP, there is no need for another request.

  • Anderson!! E-s-p-e-t-a-c-u-l-a-r guy. You obliterated my doubt. This Datalist tag is all I needed. INFINITE SUCCESS FOR YOU MY CHARÁ!!!!!

0

It seems that the variable $fracasso exists only within the while, try:

<?PHP
    var $fracasso = '';
    $consulta = mysql_query("SELECT MODELO FROM celular_banco");
    while($resultado=mysql_fetch_array($consulta))
    {
        $fracasso .= $resultado["MODELO"].',';
    }
    // Não entendi a necessidade das linhas abaixo... 
    echo $fracasso; // LGA270.AVIVBK,LGA270.ACLRBK,
    $sucesso = "LGA270.AVIVBK,LGA270.ACLRBK,";
   ?>

JS (There had been missing the JS part, should be added the echo to function properly)

<script>
var duvida = '<?PHP echo $sucesso;?>';
document.write(duvida);

var duvida = '<?PHP echo $fracasso;?>';
document.write(duvida);
</script>
  • Celso, I posted the lines below only to show that in PHP any one echoes normally, but only $success is received by JS. I’m going crazy with this...

-1

Try this:

<script>
$(document).ready(function(){
    var duvida = '<?PHP $sucesso;?>';
    document.write(duvida); 
    var duvida = '<?PHP $fracasso;?>';
    document.write(duvida);
}
</script>
  • Leandro, Inside the function, the page does not bring results even with a simple text.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.