使用AJAX将JQuery var传递给PHP [重复]

使用AJAX将JQuery var传递给PHP [重复]

问题描述:

This question already has an answer here:

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.

</div>

此问题已经存在 这里有一个答案: p>

  • 带有PHP的jQuery Ajax POST示例 13 answers span> \ n li> ul> div>

    我正在尝试传递3个看起来像这样的变量 p>

      var location = document.location.pathname; 
    var search = document.location.search; 
    var referrer = document.referrer; 
      code>  pre> 
     
     

    进入 我最终可以用来发送电子邮件的PHP文件,我之前从未使用过AJAX,但知道你可以用它来实现这个目标。 p>

    任何人都可以帮助我吗? 提前致谢。 p> div>

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']);

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.