Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    urgent help needed in javascript

    I tried to write the variable my_var into a text file but it only shows null after executing the code. Can anybody help pls?? Below is my code

    <html>
    <head>
    <script language="javascript">
    var my_var

    function WriteToFile()
    {

    my_var = 123;

    document.write(<?php $file="file.txt";
    $fh = fopen($file, 'w') or die("can't open file"); ?>;
    document.write(<?php $stringData = my_var ?>;
    document.write(<?php fwrite($fh, $stringData); ?>;
    document.write(<?php fclose($fh); ?>;
    }
    </script>
    </head>
    <body onLoad="WriteToFile();">
    <p>Hello World</p>

    </body>
    </html>

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    urgent help needed in javascript
    when do you need it by? - no point posting if the deadline has passed.

    And since it's urgent, you've probably posted on other forums as well.

    Have you received a solution from other websites yet?

  • #3
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    not yet and i only posted it in this forum

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    so when's your deadline?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    If you want people to spend time and trouble answering your question you really must read the forum rules and posting guidelines, especially those about silly thread titles and the use of code tags (surely it is obvious why those are needed?). webdev1958 is trying to tell you that your post is not urgent for us, and must await its proper turn. Why do you think that you are entitled to priority?

    Edit your post and then perhaps someone will answer you.


    It is your responsibility to die() if necessary….. - PHP Manual
    Last edited by Philip M; 03-06-2012 at 07:55 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    What a jumbled mess of PHP and antiquated JavaScript.

    <script language="php"> is a valid script tag as is <script type="application/javascript"> although since IE8 and earlier don't support JavaScript people usually use the deprecated <script type="text/javascript"> that allosw the script to run as JScript in those browsers.

    Since you have PHP available you have no need to use document.write in JavaScript since the PHP print or echo command does the exact same thing and effectively runs at the same time - before the page finishes loading.

    When combining PHP and JavaScript the PHP runs first and so must construct valid JavaScript statements that can run after.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •