1

I am having trouble saving a variable that is given a variable with JS on a server. This variable cant be stored in cookies etc. because it needs to be accessable via other computers, aswell as on page reload (So it needs to be stored on-server.)

What is the best way to do this? Is it the most easy to start learning mySQL? this seems a bit to complicated for the simple task i want it to do. Is there another method e.g. saving it to a text file on the server's side?

Bbbartt
  • 71
  • 7
  • How you store the variable is up to you, if a text file seems adequate, you'd use that. – adeneo Jan 03 '16 at 11:27
  • If you want to store something basic from the frontend (what the user sees and where you JS variable is stored) to the server, you could use a simple form. This is the most common and easiest way to store something from the frontend on the server. Then, like @adeneo said, it is us to you, how you save it. But consider if that data could be considered confidential in any way. If you do not want the user to use form, you could execute the request with JavaScript. Have a look into AJAX. jQuery wraps up the functionality pretty good for beginners ;) – func0der Jan 03 '16 at 11:36
  • if it is something that is not changing you could think of using a config-file kind scenario. – RST Jan 03 '16 at 11:38
  • U can save in cookie use php inside the javascript if yur javascript code in php file – devpro Jan 03 '16 at 11:45
  • You could do as vivoconnunxino suggests and make the file with comma separated values so you can get it back by reading the line(s) and using `explode();` to put them into an array which you access in your PHP program. – Steve Jan 03 '16 at 12:17
  • If the variable is actually created in JavaScript you would need to make a hidden input with a name and ID for example `my_var`) so you can make your JavaScript assign its value for PHP to pick up and save to your output file `document.getElementById('my_var').value = your_javascript_var;` and you would pick it up as `$_POST['my_var'];` but be sure to clean it up or you could be vulnerable to XSS attack. – Steve Jan 03 '16 at 12:27
  • This might be useful for `getElementById` http://stackoverflow.com/questions/33739624/dont-know-how-to-solve-between-php-and-javascript/33743744#33743744 and this http://stackoverflow.com/questions/33710512/get-php-variable-to-javascript/33715996#33715996 – Steve Jan 03 '16 at 13:12

1 Answers1

1

You can use ajax along with jquery so the variable is sent from the client browser to your server script:

$.ajax({
    type: 'POST',
    url: 'script_which_stores_variable.php',
    dataType: 'html',
    data: {
        'foo' : foo,
    }
});

Then, script_which_stores_variable.php can save that foo value in many different ways:

$var = $_POST['foo'];
file_put_contents('file_where_stored_value_is.php', $var);

or an insert to your database (if any).

javier_domenech
  • 5,995
  • 6
  • 37
  • 59
  • 2
    What you are presenting here is not a vanilla JS version. It would be good, if you would mention that you are using jQuery. – func0der Jan 03 '16 at 12:16