简体   繁体   English

数据未通过Ajax传递到PHP脚本

[英]Data not getting passed via Ajax to PHP script

I'm trying to send the value of a variable number via ajax to a PHP script. 我正在尝试通过ajax将变量编号的值发送到PHP脚本。 But the PHP script is not printing the desired output on opening on a browser. 但是,PHP脚本无法在浏览器上打开时输出所需的输出。 Tried but couldn't find whats wrong here. 尝试过,但在这里找不到问题。 Any pointers ? 有指针吗?

index.html index.html

<button type="submit" class="btn btn-success" id = 'first' onclick='process();'>Submit</button>

<script>

var number = 0;

function process()
{
    number++;

    var xhr;

    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    var data = "num=" + number;
    xhr.open("POST", "index.php", true);
    xhr.send(data);
}     
</script>

index.php index.php

<?php
session_start();
$number = $_POST['num'];
$_SESSION['numb'] = $number;
echo $_SESSION['numb'] ;
?>

Editing my long winded lame answer since you fixed the close curly bracket... but bloodyKnuckles is right you also need something in your 'process' function to take the response from your PHP page and output it... or whatever you want to do. 编辑我long脚的la脚答案,因为您固定了大括号...但是bloodyKnuckles是对的,您还需要在“ process”函数中添加一些内容,以从PHP页面获取响应并输出响应...或者您想要执行的任何操作。 You can basically use the method 'onreadystatechange' in the XMLHttpRequest object and then look for a 'readyState' property value of 4 which means everything is done. 您基本上可以在XMLHttpRequest对象中使用方法“ onreadystatechange”,然后查找“ readyState”属性值为4,这意味着一切已完成。 Here is a simple example of that just outputting the results to the console (which you can view using developer tools in your browser of choice). 这是一个简单的示例,仅将结果输出到控制台(您可以在选择的浏览器中使用开发人员工具进行查看)。

<script>

var number = 0;

function process()
{
number++;

var xhr;

 if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

var data = "num=" + number;
xhr.onreadystatechange = function(){            

  if (xhr.readyState==4 && xhr.status==200){
          console.log('xhr.readyState=',xhr.readyState);
          console.log('xhr.status=',xhr.status);
          console.log('response=',xhr.responseText);               
  }

  else if (xhr.readyState == 1 ){
    xhr.send(data)

  }

};

xhr.open("POST", "ajax-test.php", true);

}
</script>

As you go further you may want to update your PHP page to only update the session when the POST value is there. 当您走得更远时,您可能希望更新PHP页面以仅在POST值存在时更新会话。

<?php
//ini_set('display_errors',1);
//ini_set('display_startup_errors',1);
//error_reporting(-1);
if(isset($_POST['num'])){
            session_start();
    $_SESSION['numb'] = $_POST['num'];
    echo $_SESSION['numb'];
}
?>

You can uncomment those ini_set and error_reporting lines to try to figure out what is going on with your PHP script. 您可以取消注释那些ini_set和error_reporting行,以尝试弄清楚PHP脚本正在发生什么。

This is because you are not sending header information with request... 这是因为您没有通过请求发送标头信息...

Append this code 追加此代码

xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", data.length);
xhr.setRequestHeader("Connection", "close");

after

xhr.open("POST", "index.php", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM