A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Need help sending variables to php

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Need help sending variables to php

    I desperately need help sending variables from AS3 to PHP. I have been searching and trying for days to work it out but nothing seems to work. I have a swf which will be used for quotes, each part asks things like size, colour and other options. It has a summary part at the end that shows everything that has been added. I want to send these variables with there values to a PHP page to send an email. I can work out the PHP bit and I have tried several things to get the variables sent to the PHP but nothing seems to work.

    I want to send the variables to php when the submit button is clicked, it goes to the test PHP page which should then print the variables but it is blank. I have searched the internet for days and then I have tried lots of adjustments but still can't figure it out. I would really appreciate it if someone could check my code and let me know where I am going wrong. I am hoping the problem might be obvious to someone who knows more about it.

    Here is the code I have at the moment:
    ActionScript Code:
    Code:
    btnSubmit1.addEventListener(
      MouseEvent.MOUSE_UP,
      function(evt:MouseEvent):void {
            
            PHPSendData()
            
            var myData:URLRequest = new URLRequest("test.php")
            try {  
            navigateToURL(myData, "_self")  
            } catch (e:Error) {  
            trace(e)  
            }  
        }
    )
    
          
         function PHPSendData():void
          {
             var myData:URLRequest = new URLRequest("test.php")
             myData.method = URLRequestMethod.POST
             
                var variables:URLVariables = new URLVariables()
                    
            variables.orderEmail = txtEmailInput.text
            variables.orderType = strTankType
            variables.orderHeight = numFinalHeight
            variables.orderWidth = numFinalWidth
            variables.orderLength = numFinalLength
            variables.orderColour = arrFinalColour[globals.data.varNumColour]
            variables.orderInlet1 = strFinalInlet1
            variables.orderInlet2 = strFinalInlet2
            variables.orderOutlet1 = strFinalOutlet1
            variables.orderOutlet2 = strFinalOutlet2
            variables.orderOverflow1 = strFinalOverflow1
            variables.orderOverflow2 = strFinalOverflow2     
             
             myData.data = variables
    
             var loader:URLLoader = new URLLoader()
             loader.dataFormat = URLLoaderDataFormat.VARIABLES
             loader.addEventListener(Event.COMPLETE, doComplete)
             loader.load(myData)
          }
          
         function doComplete(anEvent:Event):void
          {
             response.text = "POST Complete = " + anEvent.target.data.submitted
          }
    Here is the PHP code for the test page:

    PHP Code:
    <?php
    $email
    =$_POST['orderEmail'];
    echo 
    "Email=" "$email";
    ?>
    I'm new to AS3 so not too sure about anything but I'm not even sure if I need the loader bit or the function doComplete but I have tried it with and without and it doesn't make a difference.

    Any help greatly appreciated, thanks.

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Going to the page using navigateToURL doesn't work like that, you're probably thinking that HTML forms send info then loads the PHP page and it shows what you sent. Doesn't work in Flash. When you send the info using URLLoader.load that's when the PHP script is executed. When you go there using navigateToURL you're just visiting that page without sending anything. So technically when you call load on the URLLoader you did send the variables and the script did echo the email, there's just no way to show it.

    As far as I know the only communication between Flash and PHP is Flash sending variables to PHP and PHP sending variables back.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Thanks for the reply

    Sorry not clear on what your saying, Are you saying I can't do it the way I am trying to? or Are you saying I can't do it at all?

    I thought there was a way to send the variables to php and then email those variables. Is there any way to do this?

  4. #4
    Senior Member
    Join Date
    Apr 2002
    Posts
    2,849
    Quote Originally Posted by 5Five555 View Post
    Going to the page using navigateToURL doesn't work like that, you're probably thinking that HTML forms send info then loads the PHP page and it shows what you sent. Doesn't work in Flash.
    Except that it does. You absolutely can submit data just like you can in HTML.

    Looking at the code above, you're redefining myData after you call PHPSendData, which is clearing it out and sending no info. Let's try something simpler:

    Code:
    btnSubmit1.addEventListener(MouseEvent.CLICK, PHPSendData);
    
    function PHPSendData(e:MouseEvent):void {
    	var myData:URLRequest=new URLRequest("test.php");
    	myData.method=URLRequestMethod.POST;
    	var variables:URLVariables = new URLVariables();
    	variables.orderEmail=txtEmailInput.text;
    	variables.orderType=strTankType;
    	variables.orderHeight=numFinalHeight;
    	variables.orderWidth=numFinalWidth;
    	variables.orderLength=numFinalLength;
    	variables.orderColour=arrFinalColour[globals.data.varNumColour];
    	variables.orderInlet1=strFinalInlet1;
    	variables.orderInlet2=strFinalInlet2;
    	variables.orderOutlet1=strFinalOutlet1;
    	variables.orderOutlet2=strFinalOutlet2;
    	variables.orderOverflow1=strFinalOverflow1;
    	variables.orderOverflow2 = strFinalOverflow2     
    	myData.data=variables;
    	navigateToURL(myData, "_self");
    }
    Does that work?

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Thanks for the reply rdoyle720

    I used the code you posted, but it still does the same thing. I get Email= but nothing after it.

    Any other suggestions? Your help is appreciated, thanks.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    OK I have been fiddling with the code a little and I added this to the end of what you posted:
    Code:
    	
             var loader:URLLoader = new URLLoader();
             loader.dataFormat = URLLoaderDataFormat.text;
             loader.addEventListener(Event.COMPLETE, doComplete);
             loader.load(myData);
          }
          
         function doComplete(anEvent:Event):void
          {
             response.text = "POST Complete = " + anEvent.target.data.submitted;
          }
    My complete code is this:
    Code:
    btnSubmit1.addEventListener(MouseEvent.CLICK, PHPSendData);
    
    function PHPSendData(e:MouseEvent):void {
    	var myData:URLRequest=new URLRequest("test.php");
    	myData.method=URLRequestMethod.POST;
    	var variables:URLVariables = new URLVariables();
    	variables.orderEmail=txtEmailInput.text;
    	variables.orderType=strTankType;
    	variables.orderHeight=numFinalHeight;
    	variables.orderWidth=numFinalWidth;
    	variables.orderLength=numFinalLength;
    	variables.orderColour=arrFinalColour[globals.data.varNumColour];
    	variables.orderInlet1=strFinalInlet1;
    	variables.orderInlet2=strFinalInlet2;
    	variables.orderOutlet1=strFinalOutlet1;
    	variables.orderOutlet2=strFinalOutlet2;
    	variables.orderOverflow1=strFinalOverflow1;
    	variables.orderOverflow2=strFinalOverflow2     
    	myData.data=variables;
    	navigateToURL(myData, "_self");
             var loader:URLLoader = new URLLoader();
             loader.dataFormat = URLLoaderDataFormat.text;
             loader.addEventListener(Event.COMPLETE, doComplete);
             loader.load(myData);
          }
          
         function doComplete(anEvent:Event):void
          {
             response.text = "POST Complete = " + anEvent.target.data.submitted;
          }
    And was a little surprised that it appears to have worked . I have tested it with php echo and all bar my last two variables are shown on the php page and are correct. I'm thinking I have to check the last two variables in the rest of my code.

    Thanks for your help. If you see any reason why I shouldn't do it this way please let me know. And thanks again I am so happy.

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