在flash中联系表单:AS3检查php是否发送了电子邮件*问题*

时间:2011-04-06 10:57:17

标签: php flash actionscript-3 contact-form

使用AS3到PHP联系闪存中的表单。

我的问题是AS3中的“showdata”函数不从PHP代码中获取变量。它无法从PHP中看到 data = success ,因此即使电子邮件已成功发送,我也总是会收到“错误”。 你可以通过建议在“showdata”或其他任何功能上改变什么来帮助我吗?

我在在线服务器上进行测试

PHP代码:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <?php
        $subject =$_POST[theme];
        $message=$_POST[msg];
        $mail_from=$_POST[email];
        $header="from: $name <$mail_from>";
        $to ='test@test.gr';
        $sending=mail($to,$subject,$message,$header);       
        if ($sending) 
            {
            echo "answer=success";
            }   
        else
            {
            echo "answer=error";
            }
    ?>
</body>
</html>

AS3代码

sendbtn.addEventListener(MouseEvent.CLICK,trysend);
function trysend(e:MouseEvent):void{
    if (subject.text == "") {
        statustext.text = "give theme";
    } else if (isValidEmail(from.text) != true) {
        statustext.text = "give email.";
    } else if (body.text == "") {
        statustext.text = "write a message";
    } else {
        var variables:URLVariables = new URLVariables();
        variables.theme = subject.text;
        variables.email = from.text;
        variables.msg = body.text;

        var urlRequest:URLRequest = new URLRequest( "mail.php" );
        urlRequest.method = URLRequestMethod.POST;
        urlRequest.data = variables;

        var loader:URLLoader=new URLLoader();
        loader.dataFormat=URLLoaderDataFormat.VARIABLES;
        loader.addEventListener(Event.COMPLETE, showData);
        loader.load(urlRequest);
    }
}

function showData(evt:Event):void {
    var loader:URLLoader=URLLoader(evt.target);
    var resulting:URLVariables = new URLVariables(loader.data);
    if (resulting.answer == "error") {      
        statustext.text = "Success!";
    } else if (resulting.answer == "success"){
        statustext.text = "error...";
        subject.text="";
        from.text="";
        body.text="";
    }
}


//Validation Email
function isValidEmail(email:String):Boolean {
    var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
    return emailExpression.test(email);
}

2 个答案:

答案 0 :(得分:0)

loader.data必须包含PHP代码回显的字符串。你必须把它放在URLVariables中才能解析它。

function showData(evt:Event):void {
    var result:URLVariables = new URLVariables(loader.data);
    if (result.data == "success") {
    ...

顺便说一句,将加载器设置为成员变量,不要在函数内部写函数;)

答案 1 :(得分:0)

现在可以这样工作:

PHP (不要将它放在'html''head''body'标签中。仅使用此代码创建文件:)

<?php
    $subject =$_POST[theme];
    $message=$_POST[msg];
    $mail_from=$_POST[email];
    $header="from: $name <$mail_from>";
    $to ='test@test.gr';
    $sending=mail($to,$subject,$message,$header);
    if ($sending){      
    print 'response=success';}
?>

<强>的ActionScript3

    function trysend(e:MouseEvent):void{
    Theme = subject.text;
    Email = from.text;
    Message = body.text;

    if (Theme == "") {
        statustext.text = "give a theme";
    } else if (Email == "") != true) {
        statustext.text = "give an email.";
    } else if (Message == "") {
        statustext.text = "give a message";
    } else {

        var variables:URLVariables = new URLVariables();
        variables.theme = Theme;
        variables.email = Email;
        variables.msg = Message;

        var urlRequest:URLRequest = new URLRequest("mail.php");
        urlRequest.method  = URLRequestMethod.POST;
        urlRequest.data = variables;

        var loader:URLLoader = new URLLoader();
        loader.load(urlRequest);
        loader.addEventListener(Event.COMPLETE, showData);
    }
}


    function showData(evt:Event):void {
        var Result:URLVariables = new URLVariables(evt.target.data);
        if (Result.response == "success") {
            statustext.text = "Success!";
            subject.text="";
            from.text="";
            body.text="";
        } else {
            statustext.text = "error";
        }
    }