为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
暮色呼如 2021-09-18 10:49:32
使用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);// Endif(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 eventfunction 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 回答

?
红糖糍粑

TA贡献1815条经验 获得超6个赞

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

 url="tempo.php"

• 一起发送您的两个参数:

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

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

 a = a_possible_value_for_a

然后直接在浏览器中调用 tempo.php(没有你的 ajax 页面)


查看完整回答
反对 回复 2021-09-18
?
30秒到达战场

TA贡献1828条经验 获得超6个赞

从 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 Implementation 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]);

 }

?>


查看完整回答
反对 回复 2021-09-18
  • 2 回答
  • 0 关注
  • 171 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信