我知道很多关于此的话题,但无论我做什么,我都无法让它工作。所以我想将一个在flash中定义的变量传递给php,并且使用一些php魔法,用这个变量做事情。现在我试图把变量"用户名"进入我的桌子。继到目前为止我得到了什么:
在闪光灯中我得到了:
connect();
function connect(){
var urlString:String = "http://[webhost]/check.php";
function Submit():void
{
var requestVars:URLVariables = new URLVariables();
requestVars.username= 10; // Dummy data to be sent to php
var urlRequest:URLRequest = new URLRequest();
urlRequest.url = urlString;
urlRequest.method = URLRequestMethod.GET;
urlRequest.data = requestVars;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
sendToURL(urlRequest)
try { loader.load(urlRequest); }
catch (error:Error) { // Handle Immediate Errors
}
}
}
function loaderCompleteHandler(e:Event):void
{
trace(e.target.data); // Response Text
}
然后我的php:
<?php
if(!empty($_POST['username'])){
$username=$_POST["username"];
}
else if(empty($username)){
$username="Unknown";
}
if (!($link=mysql_connect('localhost','[user]','[pass]')))
{
echo "Error Connecting To Database.";
exit();
}
if (!mysql_select_db('[table name]',$link))
{
echo "Error Selecting Database.";
exit();
}
try
{
mysql_query("insert into test(user) values('$username')",$link);
print "done=true";
}
catch(Exception $e)
{
print "done=$e->getMessage()";
}
echo "done=true";
?>
谢谢~ElementalVenom
答案 0 :(得分:0)
由于您正在将urlRequest.method设置为URLRequestMethod.GET,因此它将在全局$ _GET数组中传递给PHP,而不是像在PHP中那样传递到$ _POST数组中。当URLRequest的方法是“GET”时,它只是添加url-encoding参数并将其附加到URL的末尾,因此最终结果为http://example.com/check.php?username=10。
引用有关URLRequest数据属性(http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html#data)的文档:
此属性与method属性一起使用。当method的值为&gt; GET时,使用HTTP query-string&gt;语法将data的值附加到URLRequest.url的值。当方法值为POST(或GET以外的任何值)时,数据的值将在HTTP请求的正文中传输。
因此,只需更改
urlRequest.method = URLRequestMethod.GET;
到
urlRequest.method = URLRequestMethod.POST;
应该这样做。
或者,将PHP改为使用$ _GET,如下所示:
if(!empty($_GET['username'])){
$username=$_GET["username"];
}
希望这有帮助!