5

What is the best way to echo the variable get_parestotal?

Some help please! Thank you

  <script type="text/javascript">
 $(document).ready(function(){
                    var get_parestotal = 0
                $(".parestotal").each(function(){
                    get_parestotal += parseFloat($(this).text());
                });
                alert(get_parestotal);

            });

</script>



<? echo json_encode($get_parestotal); ?>
CMS
  • 704
  • 3
  • 9
  • 32
gatuso
  • 71
  • 2
  • 2
  • 8
  • You can't do that. But you can alert or console.log(get_parestotal) – andrex Sep 22 '14 at 12:43
  • 1
    the echo is in PHP server side, while the variable is on the client side... – fast Sep 22 '14 at 12:43
  • Right. Can you linking me some example using the console.log ? thanks – gatuso Sep 22 '14 at 12:44
  • https://developer.chrome.com/devtools/docs/console – Alex K. Sep 22 '14 at 12:47
  • You want to output the info to the page? Than console.log is not what you want. Learn about innerHTML or appendChild. – epascarello Sep 22 '14 at 12:47
  • Where do you want to *display* this value? If you want to show the value somewhere on the page then you'd need to identify the HTML element which should contain the value and set it as the content of that element. There is no `echo` in JavaScript. – David Sep 22 '14 at 12:48
  • Thanks guys I need to display this value on a specific div. Cann you give me a hint on innerHTML implementation for this issue? – gatuso Sep 22 '14 at 17:10

3 Answers3

7

You can log variables and so on in the console.

e.g.:

console.log(get_parestotal);

You can even concatenate witht he use of + console.log('This is your variable'+get_parestotal)

You can even add styling to your log (color, background-color,...):

console.log('%cThis is your variable'+get_parestotal+'!','color:green;');

There are some alternatives to console.log() you could use:

 console.warn();
 console.info();
 console.error();
 console.debug();
MHX
  • 1,581
  • 2
  • 21
  • 31
  • 1
    You can also just use multiple arguments, ie `console.log('bla', window, 123)`, if you use a good browser you can work with the output much better than as a string http://puu.sh/bJgmN/13103a5940.png – Philipp Gayret Sep 22 '14 at 12:57
  • Thanks guys I not properly explained the problem above. I need to display the value on a div. – gatuso Sep 22 '14 at 17:14
0

You can either do alert(myvar) or console.log(myvar).

Caveat: console.log is only supported in certain browsers...see the link.

Codex
  • 27
  • 3
0

if you want a console output than use console.log(get_parestotal);

CMS
  • 704
  • 3
  • 9
  • 32