A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [RESOLVED] What am I doing wrong to get server response?

  1. #1
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    resolved [RESOLVED] What am I doing wrong to get server response?

    Working on contact form and It's sending fine, and I can see via firebug that the server is responding success=ok but I'm not getting it in my AS3 code..

    I'm pretty much done here as well just not getting my response back from server correctly, Server is sending it my AS3 is just bad....

    PHP Code:
    //code snippet of sending data
    status_txt.text="Sending...";
            
    submit_btn.enabled=false;
            
    //first we create a URL formated variables
            
    email_data="name="+name_txt.text
                        
    +"&email="+email_txt.text
                        
    +"&subject="+subject_txt.text
                        
    +"&message="+message_txt.text;
                        
    txt1.text=email_data;
            
    //then we create a URLVariables object and load our text
            
    URL_vars=new URLVariables(email_data);
            
    //Set it to use Text
            
    URL_vars.dataFormat=URLLoaderDataFormat.TEXT;
            
    //set the URLRequest data to our variables
            
    vRequest.data=URL_vars;
            
    //Actuall send it to the server
            
    vload.load(vRequest);
            
    //wait for it to be done sending 
            
    vload.addEventListener(Event.COMPLETE,done); 
    this is working but Event.Complete is not

    PHP Code:
    function done(e:Event){
        var 
    loader=new URLLoader(e.target);
        
    email_status=new URLVariables(loader.data).success;
        
    txt1.text=email.status;
        
    status_txt.text=email_status;
        if (
    email_status=="ok"){
            
    status_txt.text="Email recieved";
            }else{
            
    status_txt.text="There was an error. not sent"+email_status;
            }    

    So what stupid mistake am I making here? I seem to remeber dealing with this issue before and it was the Event.COMPLETE being the wrong event or something.

  2. #2
    Member
    Join Date
    Jul 2005
    Location
    Brisbane, Australia
    Posts
    66
    PHP Code:
    var loader=new URLLoader(e.target); 
    Shouldn't you be casting e.target instead of creating a new URLLoader?

  3. #3
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    e.target gets me the data I've sent.
    I don't know I'm confused... I've based this from an example by someone else and this part frankly I'm not to clear on.

  4. #4
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    here's yet another snippet from another example
    PHP Code:
     private function handleComplete(event:Event):void {  
     var 
    loader:URLLoader URLLoader(event.target);  
     
    trace("Par: " loader.data.par);  
     
    trace("Message: " loader.data.msg);  
     }  
     private function 
    onIOError(event:IOErrorEvent):void {  
     
    trace("Error loading URL.");  
     } 
    and So I tried in my code.
    PHP Code:
    function done(e:Event){
    //success=ok is returned from server
        
    var loader=new URLLoader(e.target);
        
    txt1.text=loader.data
        status_txt
    .text=loader.data.success;
        if (
    loader.data.success=="ok"){
            
    status_txt.text="Email recieved";
            }else{
            
    status_txt.text="There was an error. not sent"+loader.data.success;
            }    

    my txt1 field is for testing and what I get the data that was sent not the data that was returned....

  5. #5
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    are you using php and if so do you have any import statements....did you copy the php file I find that coping the php file causes it to become corroupted and put white space in my echo's
    ~calmchess~

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I had a problem recently sending data back because of whitespace:

    echo "myResult = ".$result;

    did give no response but

    echo "myResult=".$result;

    did .
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Looks ok, I did have some problems with the PHP at first but it all seems ok. With firebug I am seeing the response


    I can't seem to get the response but looking at my code compared to several examples they look the same but I'm getting the data I sent not the data recieved... I keep thinking it's got to be something stupid I'm missing.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    StephenN was correct. Lose the 'new' in your loader line. With 'new' you are creating a new URLLoader. Without 'new' you are casting e.target to a URLLoader.

  9. #9
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    5 tons to the rescue ....stephen and him are correct
    ~calmchess~

  10. #10
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    I caught that,tried it got same result, had to run out so will clear cache and recheck when home

  11. #11
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Yep that was it, still not working but I'm seeing the response in flash, now just finding where my logic is bad, that I can handle... I knew it had to be something right in front of me that I was overlooking, I'm sure I typed new out of habit.

  12. #12
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    I just realized I never posted the final code
    Please note that there is at least one reference to a Koolmoves component. This project was actually done in the Koolmoves program
    PHP Code:
    /*
    *Not to take credit for the work of others:
    *This code is base very much of the work from
    *http://www.webdesignmo.com/blog/2008/08/14/flash-contact-form-in-actionscript-3/
    *I only adapted it and added the security checks.
    *Bret Lanius bret@bretlanius.com
    *Feel Free to use this in you projects but don't take credit for it either
    */

    //Make sure text fields are all empty and clear

    name_txt.text=email_txt.text=subject_txt.text=message_txt.text=status_txt.text="";



    //Create dragable container for our Alert box

    var dc:DragContainer = new DragContainer();

    ScriptedSkin.applyTo(dc);

    dc.header.label.text 'ALERT!';

    dc.showClose=true;

    dc.move(30,20);

    //add the alert movieclip to the container

    dc.object alert;



    //In the actionscript panel you will find this sound loaded as an assest

    //here we get access to it so we can reference it in script

    bloop=AssetManager.getAsset('bloop',false);

    energy=AssetManager.getAsset('energy',false);



    //Add events to our buttons

    submit_btn.addEventListener(MouseEvent.CLICK,submit);

    clearf_btn.addEventListener(MouseEvent.CLICK,clearForm);

    link.addEventListener(MouseEvent.CLICK,linkClick);



    //Create some variables will will be using later

    vload=new URLLoader;

    vRequest=new URLRequest('sendMail.php');

    //Note I reccomend you rename your php file something else for security

    vRequest.method=URLRequestMethod.POST;





    function 
    submit(e:MouseEvent){//This is the event for our submit button

    //first we check that no fields are blank

        
    if (name_txt.text==""||email_txt.text==""||subject_txt.text==""||message_txt.text==""){

            
    status_txt.text="Please fill ALL fields";

            
    bloop.play();

            
    stage.focus=name_txt;

            }

            
    //Now we validate the email address

            //this is much easier now that AS3 has Regex see functin later

        
    else if (!validate_email(email_txt.text)){

            
    status_txt.text="Invalid Email";

            
    bloop.play();

            
    stage.focus email_txt;

            }

    /*Now we check that they have not enter any newlines in the single line fields

    this is critical as spammers will put additional email address and headers into your form and we cannot allow that

    If spammers find a email form that is not protected they will send tons of spam from YOUR server and you can 

    get blacklisted and no longer send regular emails. Also your ISP will not be happy with you.

    */

        
    else if (hasNewLines(name_txt.text)||hasNewLines(email_txt.text)||hasNewLines(subject_txt.text)){

                
    status_txt.text="Newline detected SPAM ALERT";

                
    dc.object=alert;

                
    addChild(dc);//here we show the spam alert box and warn our user

                
    }

            

        else if (
    hasHeaders(message_txt.text)){

                
    status_txt.text="Headers are present SPAM ALERT";

                
    dc.object=alert;

                
    addChild(dc);

                }

        else {
    //If everything passes to this point we send it

            
    status_txt.text="Sending...";

            
    submit_btn.enabled=false;

            
    //first we create a URL formated variables

            
    email_data="name="+name_txt.text

                        
    +"&email="+email_txt.text

                        
    +"&subject="+subject_txt.text

                        
    +"&message="+message_txt.text;

                        
    //txt1.text=email_data;

            //then we create a URLVariables object and load our text

            
    URL_vars=new URLVariables(email_data);

            
    //Set it to use Text

            
    URL_vars.dataFormat=URLLoaderDataFormat.TEXT;

            
    //set the URLRequest data to our variables

            
    vRequest.data=URL_vars;

            
    //Actuall send it to the server

            
    vload.load(vRequest);

            
    //wait for it to be done sending 

            
    vload.addEventListener(Event.COMPLETE,done);

            }

    }







    function 
    validate_email(s){

        
    p=new RegExp;

        
    p=/^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;

        if (
    p.exec(s)==null){

            return 
    false;

            }else{

            return 
    true;

            }

    }



    function 
    done(e:Event){

    //success=ok is return on completion

        
    var vload=URLLoader(e.target);

        
    //txt1.text=">"+vload.data+"<";

        
    status_txt.text=vload.data;

        if (
    vload.data=="ok"){

            
    status_txt.text="Email recieved";

            
    energy.play();

            
    timer=new Timer(1000);

            
    timer.addEventListener(TimerEvent.TIMER,onTimer);

            
    timer.start();

            

            }else{

            
    status_txt.text="There was an error. not sent"+vload.data.success;

            }    

    }



    function 
    onTimer(e:TimerEvent){

        
    clearForm(null);

    }



    function 
    clearForm(e:Event){

        
    name_txt.text=email_txt.text=subject_txt.text=message_txt.text=status_txt.text="";

        
    submit_btn.enabled=true;

        }

        

    //Security Functions



    /*We don't allow multiline input in Name, email or subject fields

    Common way of spammers injecting headers into our form to abuse it

    */

    function hasNewLines(s){



        
    p=new RegExp;

        
    p=/(%0A|%0D|\n+|\r+)/i;

        return 
    p.test(s);

        }



    /* We don't allow any email headers like to: or cc: etc. */

        

    function hasHeaders(s){



        
    p=new RegExp;

        
    p=/(content-type:|to:|cc:|bcc:)/i;

        return 
    p.test(s);

        

    }



    function 
    linkClick(e:MouseEvent){

        
    status_txt.text="Leaving";

        
    navigateToURL(new URLRequest("http://koolmoves.com"));

        }

    stop(); 
    then the PHP is
    PHP Code:
    <?php
    $contact_name 
    $_POST['name'];
    $contact_email $_POST['email'];
    $contact_subject $_POST['subject'];
    $contact_message $_POST['message'];
    if( 
    $contact_name ) {
        
    $sender $contact_email;
        
    $receiver "you@yourdomain.com";//Change this
        
    $client_ip $_SERVER['REMOTE_ADDR'];
        
    $email_body "Name: $contact_name \nEmail: $sender \nSubject: $contact_subject \nMessage: $contact_message \nIP: $client_ip";
        
    $email_body_auto_reply ="Hello $contact_name, \nYour Email was sent from our site \nThis is the auto reply message. Thank you. \nAdmin - Bretlanius.com";
        
    $extra "From: $sender\r\n" "Reply-To: $sender \r\n" "X-Mailer: PHP/" phpversion();
        
    $extra_auto_reply "From: $receiver\r\n" "Reply-To: $receiver \r\n" "X-Mailer: PHP/" phpversion();
        
    mail$sender"Auto Reply - Re: $subject"$email_body_auto_reply$extra_auto_reply );    // auto reply mail to sender
                
    if( mail$receiver"Flash Contact Form - $contact_subject"$email_body$extra ) ){
                    echo 
    "ok";
                }
                else{
                        echo 
    "Not sent";
                }
    }
    ?>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center