Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在php文件中获取js变量_Php_Jquery_Ajax - Fatal编程技术网

无法在php文件中获取js变量

无法在php文件中获取js变量,php,jquery,ajax,Php,Jquery,Ajax,无法在msg.php中获取a $('#click01').click(function(){ var a = 14; $.ajax({ url : 'msg.php', type : 'post', data : {a:a} }).done(function() { window.location='msg.php'; }) }); msg.php var_dump($_POST['a']); // NULL, but i need

无法在
msg.php中获取
a

$('#click01').click(function(){
  var a = 14;
  $.ajax({
    url : 'msg.php',
    type : 'post',
    data : {a:a}
    }).done(function() {
      window.location='msg.php';
    })
});
msg.php

var_dump($_POST['a']); // NULL, but i need `14`

您得到的是
null
,因为AJAX调用完成后,您将用户发送到
msg.php
,其中
$\u POST
再次为空

当您这样做时:

    $('#click01').click(function () {
     var a = 14;
     $.ajax({
         url: 'msg.php',
         type: 'post',
         data: {
             a: a
         }
     }).done(function (data) {
         alert(data);
     })
 });
你会看到它的工作,你得到14


我看不出有什么好的理由首先使用AJAX发布内容,然后成功地将用户发送到同一页面。

您将获得
null
,因为AJAX调用完成后,您将用户发送到
msg.php
,其中
$\u POST
再次为空

当您这样做时:

    $('#click01').click(function () {
     var a = 14;
     $.ajax({
         url: 'msg.php',
         type: 'post',
         data: {
             a: a
         }
     }).done(function (data) {
         alert(data);
     })
 });
你会看到它的工作,你得到14


我看不出有什么好的理由首先使用AJAX发布内容,然后成功地将用户发送到同一页面。

您对AJAX请求的看法不好

$('#click01').click(function(){
  var a = 14;
  $.ajax({
    url : 'msg.php',
    type : 'post',
    data : {a:a}
    }).done(function(data) {
      // data= var_dump($_POST['a'])
      alert(data)
    })
});

在var数据中,您有ajax请求的结果,但是如果您重定向到msg.php no并没有变量,那么您对ajax请求的理解就不好了

$('#click01').click(function(){
  var a = 14;
  $.ajax({
    url : 'msg.php',
    type : 'post',
    data : {a:a}
    }).done(function(data) {
      // data= var_dump($_POST['a'])
      alert(data)
    })
});

在var数据中,您有ajax请求的结果,但是如果您重定向到msg.php no,则没有变量

,您确定您正在检查ajax返回,而不是
窗口的返回。location='msg.php'

试试这个:

$('#click01').click(function(){
  var a = 14;
  $.ajax({
    url : 'msg.php',
    type : 'POST',
    data : {'a':a},
    success: function(r) { alert(r); }
    });
});

您确定检查的是ajax返回,而不是
window.location='msg.php'的返回吗

试试这个:

$('#click01').click(function(){
  var a = 14;
  $.ajax({
    url : 'msg.php',
    type : 'POST',
    data : {'a':a},
    success: function(r) { alert(r); }
    });
});
//在msg.php中使用以下命令代替var_dump($_POST['a']): //摘录(元);;echo$a

$('document').ready(function(){var a=14

    $.ajax({
      url  : "msg.php",
      type : 'post',
      data : {a:a}
      }).done(function(data) {
          alert(data);

      });
});
//在msg.php中使用以下命令代替var_dump($_POST['a']): //提取($\u POST);回显$a

$('document').ready(function(){var a=14

    $.ajax({
      url  : "msg.php",
      type : 'post',
      data : {a:a}
      }).done(function(data) {
          alert(data);

      });
});

你怎么知道它是空的?试着打印$_REQUEST@putvande,使用
var\u dump
-
NULL
写入显示它确实使用echo或print而不是var\u dump从ajaxtry发送14您如何知道它为NULL?尝试打印$_REQUEST@putvande,使用
var\u dump
-
NULL
写入displayit14从Ajax尝试使用echo或print而不是var_dump