简体   繁体   English

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

[英]Contact form in flash: AS3 checking if php sent the email *problem*

Contact form in flash with AS3 to PHP. 用AS3到PHP在Flash中联系表单。

My problem is that the function "showdata" in AS3 does not take variables from the PHP code. 我的问题是AS3中的函数“ showdata”没有从PHP代码中获取变量。 It can't see the data=success from PHP, so i always get "error" even though the email was successfully sent. 它看不到来自PHP的data = success ,因此即使电子邮件已成功发送,我也总是会收到“错误”消息。 Can you help me by suggesting what to change in function "showdata" or anything else functional? 您可以通过建议更改函数“ showdata”或其他任何功能来帮助我吗?

I am testing on a online server 我正在在线服务器上进行测试

PHP code: 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 code 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);
}

loader.data must contain the string echoed by your PHP code. loader.data必须包含PHP代码回显的字符串。 You have to put it in a URLVariables in order to have it parsed. 您必须将其放入URLVariables中才能对其进行解析。

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

And by the way, set your loader as a member variable and don't write function inside functions ;) 顺便说一句,将您的加载程序设置为成员变量,并且不要在函数内部编写函数;)

it works now like this: 它现在像这样工作:

PHP (dont put it in 'html' 'head' 'body' tags. Create a file only with this code: ) 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 : 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";
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM