-4

I'm trying to pass 3 variables that look like this

var location = document.location.pathname;
var search = document.location.search;
var referrer = document.referrer;

Into a PHP file that I can eventually use to send emails, I have never used AJAX before but know that you could use it to achieve this.

Can anyone help me out? Thanks in advance.

Andy
  • 101
  • 10
  • Did you know that jQuery has a tutorial about Ajax? http://learn.jquery.com/ajax/ . [MDN has a tutorial as well](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest). That's what tutorials are there for: Teaching you about something you are not familiar with. – Felix Kling Nov 18 '15 at 16:16

2 Answers2

1

A simple Ajax POST method can help you here. Here's an example.

$.ajax({
            type: "POST",
            url: "ajax.php",
            data: {location: location, search: search, referrer: referrer},
            success: function(response){                    
                //do something
            }
        })//ajax end

Now in ajax.php, you can receive the values via $_POST.

PHP Receiving(ajax.php).

var_dump($_POST['location']);
Akshay
  • 2,244
  • 3
  • 15
  • 34
0

You could do like this:

    $.ajax({
        type: "POST",
        data: {'location': location,
            'search': search,
            'referrer': referrer
        },
        url: "Here the path to your php-file",
        success: function (data) {
            Here you could react on any 
        }
    });

In the php file you receive those data by Post and can handle them.