Results 1 to 9 of 9

Thread: editing javacript variable in file a through form in file b...?

  1. #1
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default editing javacript variable in file a through form in file b...?

    ok there is probably an easier way to do this but because of the way i designed the site (didnt plan very well...i know), if this is possible, id love to know how its done.

    basically i have a site where i have the same "frame" consisting of a menu, the header and footer and three tables on the right hand side stored in javascript files, which i call in the html.

    the three tables on the right contain variables which set the values therein.

    I was wondering if it was possible to create a html form which allowed me to edit those variables without having to access the server, and edit each individual js file everytime i wanted to update the tables...?

    or would i just be better off using something else like php? (which i am not good with lol)

  2. #2
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    If you are asking if JavaScript can make perminant changes to a file, the answer is no. You need to use PHP or a similar language.

    Have a look at this PHP function: fwrite()
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  3. #3
    Join Date
    Jun 2008
    Location
    Denham Springs, LA
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What you are asking is a basic tenant of javascript in general.

    If you have a "global" variable in javascript it stays available until you refresh the page across all JS files.

    Ex.

    Code:
    file1.js
    
    var myglobalVar = 20;
    
    file2.js
    
    function showMyVar()
    {
        alert(myglobalVar);
    }
    Now if you call this function from your html file you will see that the number will be displayed. So by definition if you do not want to access the server then javascript is the perfect to maintain such a variable or set of variables.

    Also, you don't even have to have multiple files. You can put all the code in one JS file or simply put it all in the head tag of the html file (Separate JS file is better though).

    Any questions, please let me know.

  4. #4
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Jas View Post
    If you are asking if JavaScript can make perminant changes to a file, the answer is no. You need to use PHP or a similar language.

    Have a look at this PHP function: fwrite()
    I looked at that, but how would I use that to replace the values given to the variables in my .js file previously?
    I can see how it would let me write to a file, but it doesnt replace the existing content.....or did I misunderstand?

    @josephhamilton1

    I dont understand how that would let me update the js files from elsewhere. Sorry, I'm still a bit of a newb at this stuff, and I probably didnt explain properly or something.

    At the moment, it works something like this:

    html that i use in all files (as this right menu is part of my "template")
    - obviously the div "rightmenu" is defined in the css file

    Code:
    <div class="rightmenu">
    <script language="JavaScript" src="rightmenu.js"></script>
    </div>
    and then the js file is like...

    Code:
    var one="news and"
    var two="stuff"
    
    document.write("<div class="newsandstuff"><center>")
    document.write("<table>")
    document.write("<tr>")
    document.write("<td>")
    document.write(one)
    document.write("</td>")
    document.write("<td>")
    document.write(two)
    document.write("</td>")
    document.write("</tr></table></div>")
    and I wanted to make a form elsewhere that would allow me to alter the value of 'one' and 'two'...if thats possible.

    So I could set up a form which has input fields for 'one' and 'two', and then I click submit and bam, it updates the values in rightmenu.js

    I'm thinking that might be impossible though...based on what Jas said. So I'll probably have to learn how to do it with PHP (any help there would be appreciated too, lol)

  5. #5
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Javascript can only make temporary changes for one user. PHP can actually change a file, thus the changes are universal.

    The easiest way to edit with fwrite would be to include one more JS file that only has the values. For example:
    Code:
    var one = "one";
    var two = "two";
    Then you would include the other file.

    With PHP, you can do something like this:
    PHP Code:
    <?php
    $values 
    "var one = \"".$_POST['FORM_INPUT_NAME_1']."\";
    var two = \""
    .$_POST['FORM_INPUT_NAME_1']."\";";

    $file fopen('./file.js','w');
    fwrite($file$values)
    fclose($file);
    ?>
    After running that PHP script, the javascript file will look something like this:
    Code:
    var one = "new value";
    var two = "new value";
    You can also have it read in your JS file and edit it, that way you wouldn't have to create a second JS file, but that would be a bit harder to do.

    I am not sure if this is what you need, so let me know.
    Last edited by Jas; 06-27-2008 at 08:09 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  6. #6
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Yeah, that looks like what I'm after.

    I just need to know how I would call the values from this new .js file into the other one. That bit confused me. Like...if I set the values in values.js, how would I get those values to be applied in rightmenu.js (which is what I use in the html)?

    The rest, I think I got.

    I make my html form, and have it send the values submitted to the php file
    the php file (with fwrite() function) then changes the values in values.js
    the values.js file then affects what is shown by rightmenu.js....right?

    Thanks for the help (and patience)

  7. #7
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    You are correct. What you do to include the values is something like this in the head section of your page:

    Code:
    <script type="text/javascript" src="./values.js"></script>
    <script type="text/javascript" src="./menu.js"></script>
    Depending on your menu, that should work. Vars in Javascript are all global unless defined otherwise, so I can't see any problems in doing the above, but let me know.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  8. The Following User Says Thank You to Jas For This Useful Post:

    methodicmadness (06-30-2008)

  9. #8
    Join Date
    Jun 2008
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Jas View Post
    You are correct. What you do to include the values is something like this in the head section of your page:

    Code:
    <script type="text/javascript" src="./values.js"></script>
    <script type="text/javascript" src="./menu.js"></script>
    Depending on your menu, that should work. Vars in Javascript are all global unless defined otherwise, so I can't see any problems in doing the above, but let me know.
    Thanks.

    I just had one more thought, but I get the impression its a stupid question...lol

    I would have to authorise these edits with my server log on and password, wouldnt I?

  10. #9
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Not really. I believe that the server will need permission to access the file, but other than that, no.

    (In other words, you should not have to log in or anything like that. PHP just needs to be able to access the file. Of course, this means you will probable want to prevent average users from getting to the script. If you need help with that, just let me know. )
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

Bookmarks

Posting Permissions

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