我如何将ajax数据存储在php变量中并重复使用?

时间:2019-06-29 08:13:20

标签: php ajax

使用ajax发送数据后,如何在PHP变量上存储那些数据?我创建了一个转储文件,在该文件中可以看到发送了该变量,但是在回显它们时看不到它们?我怎么看到他们?我通过URL发送获取数据,并通过XMLHttpRequest();发送数据,数据返回的很好,但是为什么它不存储在PHP变量中呢?

<?php
//dumping code to see received data
$output = "Post Variables\n";
$output .= print_r($_POST, true);

$output .= "\nGet Variables\n";
$output .= print_r($_GET, true);

$output .= "\nBody Content\n";
$output .= print_r(file_get_contents('php://input') ?: "empty", true);

file_put_contents("dump.txt", $output);
// End


if(isset($_GET['a'])) {
    die('This is post data: ' . htmlspecialchars($_GET['a']));
 
} 
if(isset($_POST['b'])) {
    die('This is post data: ' . htmlspecialchars($_POST['b']));
 
} 
    echo "This is get variable: " .$a;
    echo "This is post variable: " .$b;

?>


<html>
<head>
<script>
//sending ajax request to change table name on onclick event
function clickMe(j){
    // Create our XMLHttpRequest object
    var req = new XMLHttpRequest();
    // Create some variables we need to send to our PHP file
    var dayName = document.getElementById("btn"+j).value;
    var SVAR = "b="+dayName;
    var url = "tempo.php?a="+dayName;
    req.open("POST", url, true);
    // Set content type header information for sending url encoded variables in the request
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    // Access the onreadystatechange event for the XMLHttpRequest object
    req.onreadystatechange = function() {
	    if(req.readyState == 4 && req.status == 200) {
		    let data_return = req.responseText;
            document.getElementById("status1").innerHTML = data_return;
 
	    }
    }
    // Send the data to PHP now... and wait for response to update the status div
    req.send(SVAR);
    
}
</script>
</head>
<body>
<h2>Ajax Post to PHP and Get Return Data</h2>


<button id="btn1" value="saturday"  onclick="clickMe(1)">btn1</button>
<button id="btn2" value="sunday"  onclick="clickMe(2)">btn2</button>

<br><br>
<div id="status1"></div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

使用XMLHttpRequest的方式不正确。您应该使用2个不同的页面:调用者(index.php)和异步脚本(tempo.php) 要更正您当前的呼叫者页面:index.php: •使用不带任何参数的网址:

 url="tempo.php"

•一起发送两个参数:

 req.send("a="+dayName+"&b="+dayName);

要调试异步页面:tempo.php,只需在tempo.php的顶部添加一个伪造的get_parameter:

 a = a_possible_value_for_a

,然后直接在浏览器中调用tempo.php(无ajax页)

答案 1 :(得分:0)

从HTML文件发送的请求。

发送过程一:

  $(document).on("click","#btn1",function(){
   var data = $(this).val();

   /* ajax request sent start */
   $.ajax({
       method:"post",
       url:"phpFileName.php",
       data:{backendPostName:data},
       dataType:"json",
       success:function(response){
           /* Logic implemented here */
       }
   });
  /* ajax request sent end*/
});

根据您的html结构发送第二个过程:

function clickMe(data){
   var data = $(this).val();

  /* ajax request sent start */
   $.ajax({
       method:"post",
       url:"phpFileName.php",
       data:{backendPostName:data},
       dataType:"json",
       success:function(response){
           /* Logic Impliment here */
       }
   });
  /* ajax request sent end*/

}

当您想在php文件中接收此发送数据时。

首先检查是否通过php“ isset()”函数找到了该名称

以下示例:

php文件:

<?php 
   if(isset($_POST['backendPostName'])){
     $customName = $_POST['backendPostName'];

     /* 
        store or other logic implement here .
        if you wants to echo html then echo "success"; or your choice
        if you wants to return json data then return json_encode(["result"=>1]);
      */

     /* For HTML Return */

    echo "<h1>Success</h1";

    /*For Json return */   

    echo json_encode(["result"=>1]);
 }

?>