第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將 ajax 數(shù)據(jù)存儲(chǔ)在 php 變量上并重用它?

如何將 ajax 數(shù)據(jù)存儲(chǔ)在 php 變量上并重用它?

PHP
暮色呼如 2021-09-18 10:49:32
使用ajax發(fā)送數(shù)據(jù)后如何將它們存儲(chǔ)在PHP變量中?我創(chuàng)建了一個(gè)轉(zhuǎn)儲(chǔ)文件,我可以在其中看到該變量已發(fā)送,但是當(dāng)我回顯它們時(shí)卻看不到它們?我怎么能看到他們?我通過(guò) URL 發(fā)送獲取數(shù)據(jù)并通過(guò)XMLHttpRequest(); 數(shù)據(jù)發(fā)布數(shù)據(jù)返回很好但是為什么它不存儲(chǔ)在 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貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超6個(gè)贊

你使用 XMLHttpRequest 的方式不對(duì)。您應(yīng)該使用 2 個(gè)不同的頁(yè)面:調(diào)用者 (index.php) 和異步腳本 (tempo.php) 更正您當(dāng)前的調(diào)用者頁(yè)面: index.php : ? 使用不帶任何參數(shù)的 url:

 url="tempo.php"

? 一起發(fā)送您的兩個(gè)參數(shù):

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

要調(diào)試異步頁(yè)面:tempo.php,只需在 tempo.php 頂部添加一個(gè)偽造的 get_parameter:

 a = a_possible_value_for_a

然后直接在瀏覽器中調(diào)用 tempo.php(沒(méi)有你的 ajax 頁(yè)面)


查看完整回答
反對(duì) 回復(fù) 2021-09-18
?
30秒到達(dá)戰(zhàn)場(chǎng)

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊

從 HTML 文件發(fā)送的請(qǐng)求。


發(fā)送過(guò)程一:


  $(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*/

});

根據(jù)你的html結(jié)構(gòu)發(fā)送過(guò)程二:


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*/


}

當(dāng)您想在 php 文件中接收此發(fā)送數(shù)據(jù)時(shí)。


首先通過(guò)php“isset()”函數(shù)檢查是否找到這個(gè)名字


下面的例子:


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

 }

?>


查看完整回答
反對(duì) 回復(fù) 2021-09-18
  • 2 回答
  • 0 關(guān)注
  • 205 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)