[英]Passing variable from AS3 to PHP
我目前正在做一個 Flash 游戲,記錄所用的時間並將其發送到 php。 目前,我的游戲正在運行,並且可以正確跟蹤所花費的時間。 URLLoader 也工作正常,但是,我的 php 頁面似乎無法從 $_POST 獲得結果。 我已經在下面發布了我的代碼。
我將不勝感激任何幫助。 已經嘗試了各種方法,但都無法奏效。 我要么在單獨的窗口上打開 processScore.php 並顯示空頁面,要么什么也沒有發生。 提前致謝!
AS3:
function sendTime() {
var req:URLRequest = new URLRequest("processScore.php");
var loader:URLLoader = new URLLoader();
//using URLLoaderDataFormat.VARIABLES give me error so I changed to TEXT.
loader.dataFormat = URLLoaderDataFormat.TEXT;
var toSend:URLVariables = new URLVariables();
req.data = toSend;
req.method = URLRequestMethod.POST;
toSend.timeRecorded = timeTaken;
loader.addEventListener(Event.COMPLETE, timeSent);
loader.load(req);
}
function timeSent(e:Event) {
trace("SENT");
}
processScore.php:
<?php
if(isset($_POST['timeRecorded'])) {
echo $_POST['timeRecorded'];
}
?>
嘗試這個
function sendTime() {
var loader:URLLoader = new URLLoader();
var req:URLRequest = new URLRequest("processScore.php");
var toSend:URLVariables = new URLVariables();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
req.method = URLRequestMethod.POST;
toSend.timeRecorded = timeTaken;
req.data = toSend;
loader.addEventListener(Event.COMPLETE, timeSent);
loader.load(req);
}
function timeSent(e:Event) {
trace("SENT");
}
PHP:
<?php
$timeRecorded = $_POST['timeRecorded'];
echo $timeRecorded;
?>
設法解決了問題。 我沒有真正弄清楚 URLLoader 背后的主要思想,因此我認為 Flash 頁面保留時存在問題。 主要問題是絕對的 Url 問題,我沒有做任何事情來正確處理 php 中的結果。 感謝大家提出問題。 我很欣賞!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.