开发者

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

Contact form in flash with AS3 to PHP.

My problem is that the function "showdata" in AS3 does not take variables from the PHP code. It can't see the data=success from PHP, so i always get "error" even though the email was successfully sent. Can you help me by suggesting what to change in function "showdata" or anything else functional?

I am testing on a online server

PHP code:

    <!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

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();
        开发者_如何学Cloader.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. You have to put it in a URLVariables in order to have it parsed.

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
    $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";
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜