猿问

如何将 ajax 数据存储在 php 变量上并重用它?

使用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>


暮色呼如
浏览 172回答 2
2回答

红糖糍粑

你使用 XMLHttpRequest 的方式不对。您应该使用 2 个不同的页面:调用者 (index.php) 和异步脚本 (tempo.php) 更正您当前的调用者页面: index.php : • 使用不带任何参数的 url:&nbsp;url="tempo.php"• 一起发送您的两个参数:&nbsp;req.send("a="+dayName+"&b="+dayName);要调试异步页面:tempo.php,只需在 tempo.php 顶部添加一个伪造的 get_parameter:&nbsp;a&nbsp;=&nbsp;a_possible_value_for_a然后直接在浏览器中调用 tempo.php(没有你的 ajax 页面)

30秒到达战场

从 HTML 文件发送的请求。发送过程一:&nbsp; $(document).on("click","#btn1",function(){&nbsp; &nbsp;var data = $(this).val();&nbsp; &nbsp;/* ajax request sent start */&nbsp; &nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp;method:"post",&nbsp; &nbsp; &nbsp; &nbsp;url:"phpFileName.php",&nbsp; &nbsp; &nbsp; &nbsp;data:{backendPostName:data},&nbsp; &nbsp; &nbsp; &nbsp;dataType:"json",&nbsp; &nbsp; &nbsp; &nbsp;success:function(response){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* Logic implemented here */&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;});&nbsp; /* ajax request sent end*/});根据你的html结构发送过程二:function clickMe(data){&nbsp; &nbsp;var data = $(this).val();&nbsp; /* ajax request sent start */&nbsp; &nbsp;$.ajax({&nbsp; &nbsp; &nbsp; &nbsp;method:"post",&nbsp; &nbsp; &nbsp; &nbsp;url:"phpFileName.php",&nbsp; &nbsp; &nbsp; &nbsp;data:{backendPostName:data},&nbsp; &nbsp; &nbsp; &nbsp;dataType:"json",&nbsp; &nbsp; &nbsp; &nbsp;success:function(response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/* Logic Implementation here */&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;});&nbsp; /* ajax request sent end*/}当您想在 php 文件中接收此发送数据时。首先通过php“isset()”函数检查是否找到这个名字下面的例子:php 文件:<?php&nbsp;&nbsp; &nbsp;if(isset($_POST['backendPostName'])){&nbsp; &nbsp; &nbsp;$customName = $_POST['backendPostName'];&nbsp; &nbsp; &nbsp;/*&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; store or other logic implement here .&nbsp; &nbsp; &nbsp; &nbsp; if you wants to echo html then echo "success"; or your choice&nbsp; &nbsp; &nbsp; &nbsp; if you wants to return json data then return json_encode(["result"=>1]);&nbsp; &nbsp; &nbsp; */&nbsp; &nbsp; &nbsp;/* For HTML Return */&nbsp; &nbsp; echo "<h1>Success</h1";&nbsp; &nbsp; /*For Json return */&nbsp; &nbsp;&nbsp; &nbsp; echo json_encode(["result"=>1]);&nbsp;}?>
随时随地看视频慕课网APP
我要回答