0

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?

Merrin K
  • 1,602
  • 1
  • 16
  • 27
Praveen Kumar
  • 849
  • 8
  • 8

1 Answers1

0

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

Somen
  • 46
  • 5