How to display php variable in html

I just want to know how can I display a javascript variable in html?

Below is my PHP code:

var duration = <? echo $postduration; ?>;

(The variable above uses php to get the php variable)

thank

+5
source share
5 answers

Make the code more concise with jQuery :

(1) Add this to your <head>:

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

(2) Create the element in which you want to display the variable and specify an identifier for it, for example:

<span id="printHere"></span>

(3) Add this to your JavaScript:

var duration="<?php echo $postduration ?>";
$('#printHere').html(duration);

For your PHP, try the following two options:

<?=$postduration?>

or...

<?php echo $postduration; ?>
+8
source

Why not use only displaying the php variable directly in html?

$postduration echos "some." javascript "some":

Here is <script>document.write(duration)</script> text.

:

Here is some text.
+8
document.getElementById('yourElementId').innerHtml = duration;
+3

DIV DIV, PHP var:

 <script>
 var duration = <? echo $postduration; ?>;
 var div = document.createElement("DIV");
 div.appendChild(document.createTextNode(duration));
 document.body.appendChild(div);
 </script>
+2

, , . , javascript "" - ( php, - ). Php javascript, , $postduration 5, var duration = 5;, postduration = "test", var duration = test; ( javascript .

, javascript html, - ( innerHTML - ), .

Example here: http://jsfiddle.net/SLbKX/

0
source

All Articles