JavaScript Magento saving data in session but in Javascript [duplicate] magento save data in custom table,magento save data in

I have this code:

<script type="text/javascript">
    var foo = 'bar';
    <?php
        file_put_contents('foo.txt', ' + foo + ');
    ?>

    var baz = <?php echo 42; ?>;
    alert(baz);
</script>

Why does this not write "bar" into my text file, but alerts "42"?


NB: Earlier revisions of this question were explicitly about PHP on the server and JavaScript on the client. The essential nature of the problem and solutions is the same for any pair of languages when one is running on the client and the other on the server (even if they are the same language). Please take this in to account when you see answers talking about specific languages.

Answer:1

Your Javascript will execute on the client, not on the server. This means that foo is not evaluated on the server side and therefore its value can't be written to a file on the server.

The best way to think about this process is as if you're generating a text file dynamically. The text you're generating only becomes executable code once the browser interprets it. Only what you place between <?php tags is evaluated on the server.

By the way, making a habit of embedding random pieces of PHP logic in HTML or Javascript can lead to seriously convoluted code. I speak from painful experience.

Answer:2

In web application every task execute in a manner of request and response.

Client side programming is with html code with Java script and its frameworks, libraries executes in the internet explorer, Mozilla, chrome browsers. In the java scenario server side programming servlets executes in the Tomcat, web-logic , j boss, WebSphere severs

Answer:3

I want to design a helper function which would try to execute passed as parameter deep property path and to return value extracted by it. For example consider following code var foo = { bar: ...

I want to design a helper function which would try to execute passed as parameter deep property path and to return value extracted by it. For example consider following code var foo = { bar: ...

  1. javascript create universal function

How to dismiss Bootstrap popover by clicking outside the popover. Currently it has toggle for open link. HTML <div class="widget-rating"> <span class="rateit rating-average">&...

How to dismiss Bootstrap popover by clicking outside the popover. Currently it has toggle for open link. HTML <div class="widget-rating"> <span class="rateit rating-average">&...

  1. dismiss bootstrap popover programmatically
  2. dismiss bootstrap popover
  3. dismiss bootstrap popover on click
  4. bootstrap dismiss popover click outside
  5. bootstrap popover dismiss button
  6. bootstrap popover dismiss on next click
  7. bootstrap popover dismiss not working
  8. popover dismiss bootstrap 4
  9. bootstrap popover dismiss event
  10. popover dismiss bootstrap 3
  11. bootstrap popover dismiss ios

I am develop an app using GWT Maps Api V3. I try create Place search box like example of Google Maps JavaScript API(link Place search box - Google Maps JavaScript API ) I have viewed the javascript ...

I am develop an app using GWT Maps Api V3. I try create Place search box like example of Google Maps JavaScript API(link Place search box - Google Maps JavaScript API ) I have viewed the javascript ...

What is the difference between I know here the global jQuery is passed as $ to function, (function($){ })(jQuery); and this one $(function(){ })();

What is the difference between I know here the global jQuery is passed as $ to function, (function($){ })(jQuery); and this one $(function(){ })();

  1. anonymous function javascript es6
  2. anonymous function javascript example
  3. anonymous function javascript arrow
  4. anonymous function javascript mdn
  5. anonymous function javascript this
  6. anonymous function javascript error
  7. anonymous function javascript syntax
  8. anonymous function javascript onclick
  9. anonymous function javascript w3schools
  10. anonymous function javascript arguments
  11. anonymous function javascript advantages
  12. anonymous function javascript self executing
  13. anonymous function javascript with parameter
  14. anonymous function javascript callback
  15. anonymous function javascript use
  16. anonymous function javascript return
  17. anonymous function javascript scope
  18. anonymous function javascript pass variable
  19. anonymous function javascript call
  20. anonymous function javascript la gi