How to use a PHP variable in javascript?

Asked

Viewed 45,121 times

6

You can declare a variable in PHP and then use it in Javascript?

Example:

<?
    var w_qtd_v = 0;
?>

<script language="javascript">
    w_qtd_v = w_qtd_v + 1;
</script>
  • You can play the php value in a Hidden input or pass this value to a js function. This js needs to run when the page loads or at another event?

  • The first block is php? It doesn’t seem.

  • 1

    <script> var variableJavascript = "<? php echo variavelPHP ? >"; </script>

  • @bfavaretto, the first block was just an example of how I want to "be"

  • @lost js will run when I call a Function, to have an assignment. That’s why I wanted to declare the variable in PHP so it doesn’t reset whenever I enter JS

5 answers

6


Javascript cannot update the value of the PHP variable, only php manipulates/creates JS. To do this update, you will need an ajax, right after you have changed the value of the variable with JS...if that is the question.

PHP runs on the server side to create the page, and JS runs on the client side with the page already created. The only way they "talk" is via ajax.

If you are using a framework, such as zend, etc., you usually use a template engine to work with variables in HTML, and you can also pass these variables to JS, an example of variable use with the template engine Raintpl

index php.

// include
require "library/Rain/autoload.php";
// namespace
use Rain\Tpl;

$config = array(
    "tpl_dir"   => "vendor/rain/raintpl/templates/test/",
    "cache_dir" => "vendor/rain/raintpl/cache/"
);

Tpl::configure( $config );

$t = new Tpl;
$t->assign('title','Hello!');
$t->draw('test');

test.html

<html>
...
<p>
{$title}
</p>
</html>

<script language="javascript">
    var title = "{$title}";
</script>

or just do it like this

<?php

$title = "Hello";

?>
<script language="javascript">
    var title = "<?php print $title; ?>";
</script>
  • Very good what happened to me William! But as in my case I intend to use the variable declared in PHP as an iterator, as would be?

  • I don’t know if I understand, but with template engine, you pass the variable value by the $t->assign('title','Hello!'); where the first parameter is the variable name and the second is its value, assign tbm can receive an array

  • 1

    javascript has no way to update the value of the PHP variable, only php manipulates JS, to do this update, you will need an ajax, soon after you have changed the value of the variable with JS...if that is the question.

  • Huum' now yes you answered my doubt !

  • 1

    @Williamborba added his comment and a few more words in the reply. Correct if you find wrong.

  • @Alexander if William answered your question you can mark the answer as accepted.

  • He replied in the comment! Not in the answer area ^^

  • @Sergio vlw, thank you for contributing in reply :)

Show 3 more comments

3

Try it like this:

<?php
$variavel = "texto";
?>


<script>
  var variavel = "<?php echo $variavel; ?>";
</script>
  • 2

    Rene, it would be ideal to put together an explanation as to why the question code doesn’t work. That is, talk about the difference between PHP and Javascript in terms of client/server side.

  • I agree, @Sergio. I see a lot of questions around without any information. I found this for example by searching for "Try like this"

2

PHP and Apache are responsible for interpreting the php page and sending back to client, user browser, html code and in this process php can send javascript code within html according to processing.

However, depending on the case, this can be done through an AJAX request where the JS will send a request to the server and get a response, asynchronously, without interrupting your application. And then, with that answer in JS, you can do whatever you want.

References:

1

You can use pass the following way (example by calling a PHP method to pass to JS):

<?php 
        $usuario = AutenticacaoUsuarioLogado::usuariologado();

        echo "<script type='text/javascript'>";
        echo "    var usuario = " . $usuario . ";";
        echo "</script>";        
?>

0

tbm can try like this

<?php
$nameemphp = "Ola mundo";
echo "<script>var nameemjs = '".$nameemphp."';</script>";
?>

It assigns a php variable to a variable in javascript, using concatenation to add the variable in echo.

Browser other questions tagged

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