Asked  7 Months ago    Answers:  5   Viewed   33 times

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

Below is my php code:

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

(The variable above uses php to retrieve a php variable)

Thank you

 Answers

57

Make your code more concise with jQuery:

(1) Add this in your <head>:

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

(2) Create an element where you want the variable to be displayed, and give it an ID, e.g.:

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

(3) Add this in your JavaScript:

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

For your PHP, try the following two options:

<?=$postduration?>

or...

<?php echo $postduration; ?>
Wednesday, March 31, 2021
 
Rudie
answered 7 Months ago
30

If both of your function are residing in the same class you can call a function inside another function using $this as pointed in other answers

class myclass{

public $var1 = 121;
    public function foo($postid){
        echo $postid;
    }
    public function bar(){
        $this->foo($this->var1);
    }
}
$objectmyclass = new myclass();
$objectmyclass->bar();

OR

class myclass{

public $var1 = 122;
    public function foo($postid){
        echo $postid;
    }
    public function bar($postid){
        $this->foo($postid);
    }
}
$objectmyclass = new myclass();
$objectmyclass->bar($objectmyclass->var1);

both ways you will be able to execute the statements inside the function foo hope this helps

Wednesday, March 31, 2021
 
Blur
answered 7 Months ago
61

Why you are not using json_encode

<?php
 $data = array( );
 foreach (glob($queryglob) as $filename) $data[$filename] = array( 'caption' => '' );
?>
<script type="text/javascript">
 var data = <?php echo json_encode( $data ); ?>
</script>

That is much more clever than building a string

Saturday, May 29, 2021
 
altexpape
answered 5 Months ago
45

Based on your screenshot of Settings (Preferences on Mac) | Editor | Language Injections.

Please delete 3rd language injection rule from the bottom (the one for "div" -- that has "IDE" in Scope column).

That rule injects HTML into div tag which tells IDE to treat all other code (even PHP) inside such tag as HTML/plain text.

Thursday, August 5, 2021
 
Nil
answered 3 Months ago
Nil
80

A recent change in PHPMailer allows Debugoutput to be a closure, so you can make it do whatever you like, for example to collect all the debug output and emit it later:

$debug = '';
$mail->Debugoutput = function($str, $level) {
    $GLOBALS['debug'] .= "$level: $strn";
};
//...later
echo $debug;
Monday, August 16, 2021
 
samayo
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :