簡體   English   中英

為什么這個 JS 和 PHP 代碼不起作用? (我正在嘗試將值從 JS 發送到 PHP)

[英]Why doesn't this JS & PHP code work? (I am trying to send a value from JS to PHP)

我有一個名為sample.php的文件,其中包含此代碼,其任務是通過 AJAX 將變量值從 JS POST 到 PHP。 這是代碼:

<!DOCTYPE html>
<html lang="en">
<head>
  <script type="text/javascript">
    var ThunkableWebviewerExtension = {
        receiveMessage: function(fxn) {
            var callbackFunction = function(event) {
                if (typeof fxn === 'function') {
                    fxn(event.data)
                }
            };
            document.addEventListener('message', callbackFunction, false);
            window.addEventListener('message', callbackFunction, false);
        }
    }
  </script>
</head>

<body>
<script type="text/javascript">
  var value;
  ThunkableWebviewerExtension.receiveMessage(function(message) {
      value = message; //this is the value I want to send to PHP
  });
  
//sending the value with ajax (POST)

  return new Promise((resolve, reject) => {
        $.ajax({
          url : "./sample.php", //same file
          method : "POST",
          data: {"data": value},
          
          success : (res) => {
            resolve(res)
          },
          error : (res) => {
            reject(res)
          }
        })
  })
</script>

<?php
echo $_POST['data']; //nothing comes here...
?>

</body>
</html>

我唯一的目標是在 PHP 中訪問value變量的值(在 JS 端)。 我開始知道這可以用 AJAX 來完成,所以我試過了,但沒有用 - PHP 代碼沒有回顯任何東西......問題在哪里?

替代方法(用於在 PHP 中訪問 JS 變量的值)非常受歡迎。 :)

首先,為了運行 ajax 函數,您需要jQuery 然后你必須從 ajax 調用中刪除promise ,因為根據文檔- “$.ajax() 返回的 jqXHR 對象從 jQuery 1.5 開始實現了 Promise 接口,為它們提供了 Promise 的所有屬性、方法和行為”

<!DOCTYPE html>
<html lang="en">
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<head>
  <script type="text/javascript">
    var ThunkableWebviewerExtension = {
        receiveMessage: function(fxn) {
            var callbackFunction = function(event) {
                if (typeof fxn === 'function') {
                    fxn(event.data)
                }
            };
            document.addEventListener('message', callbackFunction, false);
            window.addEventListener('message', callbackFunction, false);
        }
    }
  </script>
</head>

<body>
<script type="text/javascript">
  var value ;
  ThunkableWebviewerExtension.receiveMessage(function(message) {
      value = message; //this is the value I want to send to PHP
  });
  
//sending the value with ajax (POST)
$.ajax({
          url : "./sample.php", //same file
          method : "POST",
          data: {"data": value},
          
          success : (res) => {
            console.log(res);
          },
          error : (res) => {
            console.log(res);
          }
        })
</script>
<?php
echo $_POST['data']; //nothing comes here...
?>
</body>
</html>

如果您在添加此解決方案后出現Undefined Index: data錯誤,您可以在您的 JS 代碼中的value變量中添加一個示例值。

暫無
暫無

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

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