简体   繁体   中英

How can I save javascript variable value in a file using PHP or any other language?

I would like to save a JavaScript variable value in a file.

For example,

<script>
    var ref = document.referrer;
</script>

I would like to save the variable ref value in a text file using PHP.

<?php
    $file = fopen("ref.txt", "a+") or die("Unable to open file!");
    fwrite($file, ref);
    fclose($file);
?>

How can I do it?

You can do this by --

  1. AJAX: Using AJAX you can easily pass the JS variable to PHP.

  2. GET/POST: first you have to create a form with a text box. Then you have to set the value of that text box from your JS code like $('#id').val(ref) . AND then you have to submit the form to a PHP page where you can get the value by simple calling $_GET['data'] or $_POST['data'] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM