使用Jquery AJAX将Javascript变量转换为PHP

问题描述:

我具有以下javascript函数

I have the following javascript function

function success_callback(p)
    {

        lat = p.coords.latitude.toFixed(2);
        lon = p.coords.longitude.toFixed(2);

    }

现在我想使用Jquery AJAX将两个变量都传输到PHP,我对Jquery还是很陌生,我不知道该怎么做.我想将变量传输到该JS代码所在的同一个PHP文件中.有可能吗?

Now I want to transfer both the variable to PHP using Jquery AJAX, I am pretty new to Jquery, I am not able to figure out how to do it. I want to transfer the variables to the same PHP file where this JS code resides. Is that possible ?

是的.您可以使用数据字符串发布变量.看看手册.

Yes it is. You could post the variables using the data string. Have a look at the Manual.

$.ajax({
  type: "POST",
  data: "lat="+lat+"&lon="+lon,
  success: function(){
      //callback code
      alert("Done!");
  }
});