2014-03-13 24 views
0

我想創建一個客戶端的swf文件,該文件使用php將文本字段的數據發送到我的服務器上的數據庫。我可以讓swf與另一臺服務器上的php文件進行通信嗎?

我的代碼工作正常,當我上傳到我的服務器上,但我想發送swf文件到客戶端,並讓swf與我的服務器上的php文件進行通信。

我該做些什麼來完成這項工作?即使我不知道客戶端放置swf的域名,這是否可能?

這是我的AS3代碼:

// Assign a variable name for our URL Variables object 
var variables:URLVariables = new URLVariables(); 

// Build the varSend variable 
var varSend:URLRequest = new URLRequest("databaseTest.php"); 
varSend.method = URLRequestMethod.POST; 
varSend.data = variables; 

// Build the varLoader variable 
var varLoader:URLLoader = new URLLoader; 
varLoader.dataFormat = URLLoaderDataFormat.VARIABLES; 
varLoader.addEventListener(Event.COMPLETE,completeHandler); 


// Handler for PHP script completion and return 
function completeHandler(event:Event):void{ 
    //clear the form fields 
    name_txt.text = ""; 
    lastName_txt.text = ""; 
    mail_txt.text = ""; 
    status_txt.text = "Thanks"; 
} 

// Add an event listener for the submit button and what function to run 
submit_btn.addEventListener(MouseEvent.CLICK,ValidateAndSend); 

// Validate form fields and send the variables when submit button is clicked 
function ValidateAndSend(event:MouseEvent):void { 

    // validate all the form fields 
    if(!name_txt.length){ 
     status_txt.text = "name"; 
    }else if(!lastName_txt.length){ 
     status_txt.text = "last name"; 
    }else if(!mail_txt.length){ 
     status_txt.text = "mail"; 
    }else{ 
     // All is good so send the message to the parse file 

     //Ready the variables for sending 
     variables.comType = "parseInformation"; 
     variables.sendName = naam_txt.text; 
     variables.sendLastName = achternaam_txt.text; 
     variables.sendEmail = mail_txt.text; 

     //Send the data to the php file 
     varLoader.load(varSend); 
    } 
} 

,這是PHP代碼:

<?php 

// Connect to MySQL database 
mysql_connect("localhost","*******","********") or die (mysql_error()); 
mysql_select_db("*******") or die (mysql_error()); 

if($_POST['comType'] == "parseInformation") { 
    $name = $_POST['sendName']; 
    $lastName = $_POST['sendLastName']; 
    $email = $_POST['sendEmail']; 

    $sql = mysql_query("INSERT INTO databaseTest (name, lastName, mail) 
    VALUES('$name', '$lastName', '$email')") 
    or die (mysql_error()); 

    mysql_close(); 
    exit(); 
} 

?> 

回答

1

您必須在您將PHP代碼在服務器上添加文件。

它必須命名爲crossdomain.xml,並且必須放在服務器的根目錄中。在該文件中,你可以寫允許域:

<?xml version=」1.0″?> 
<!DOCTYPE cross-domain-policy SYSTEM 「http://www.adobe.com/xml/dtds/cross-domain-policy.dtd」> 
<cross-domain-policy> 
     <allow-access-from domain="domain/IP address" /> 
</cross-domain-policy> 

或者,因爲你不知道他們,這讓每個人:

<allow-access-from domain="*" /> 

(不要讓任何人訪問如果可能的話)

相關問題