PDA

View Full Version : editing javacript variable in file a through form in file b...?



methodicmadness
06-25-2008, 10:23 PM
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)

Jas
06-26-2008, 06:55 PM
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() (http://us.php.net/manual/en/function.fwrite.php)

josephhamilton1
06-27-2008, 03:29 AM
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.


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.

methodicmadness
06-27-2008, 06:52 PM
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() (http://us.php.net/manual/en/function.fwrite.php)

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


<div class="rightmenu">
<script language="JavaScript" src="rightmenu.js"></script>
</div>


and then the js file is like...



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)

Jas
06-27-2008, 07:58 PM
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:


var one = "one";
var two = "two";


Then you would include the other file.

With PHP, you can do something like this:


<?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:

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.

methodicmadness
06-29-2008, 01:11 AM
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)

Jas
06-29-2008, 10:11 PM
You are correct. What you do to include the values is something like this in the head section of your page:


<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.

methodicmadness
06-30-2008, 08:33 PM
You are correct. What you do to include the values is something like this in the head section of your page:


<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?

Jas
07-01-2008, 12:14 AM
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. :))