簡體   English   中英

我如何將ajax數據存儲在php變量中並重復使用?

[英]How can i store ajax data on a php variable and reuse it?

使用ajax發送數據后,如何將那些存儲在PHP變量上? 我創建了一個轉儲文件,在該文件中可以看到發送了該變量,但是在回顯它們時看不到它們? 我怎么看到他們? 我通過URL發送獲取數據,並通過XMLHttpRequest();發布數據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> 

您使用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頁)

從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]);
 }

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM