A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Insert selected info to database

  1. #1
    Member
    Join Date
    Nov 2014
    Posts
    34

    Insert selected info to database

    Hi guys. I'm sorry for asking I'm just running out of time.

    So i'm trying to make something like this: http://www.bakedbymelissa.com/checko...erCreator.aspx

    For now, I have this: stage.png
    the square represents the cupcake base. the circles are movieclips too. The as layer has the code to tween what square appears. My problem is, if i click the circle beside chocolate, a text/string "chocolate" must be saved in a database field..same goes if the others are clicked but only one. Assuming there is a submit button. I use mysql. Can you show me how?

    The code so far:

    Hi guys I'm making something like this: [Cupcake Creator][1]

    For now, I have this: ![window][2]

    I'm going to put this in a php page. I have 4 squares and 4 circles.all movieclips.the flavors are just static texts.the as layer contains the code to tween what square should appear.that was solved in my previous question here.

    Here's what I want to achieve:
    If I click chocolate or vanilla, or any of those four, it should be saved in a database when the button submit is clicked(assuming there is already a button).I use mysql. I don't know how to get those strings I'm in a rush so i don't have time to try everything on google. Please help. noob here.
    [1]: http://www.bakedbymelissa.com/checko...erCreator.aspx
    [2]: http://i.stack.imgur.com/I6MLq.png


    The as code so far:

    Code:
        import flash.display.MovieClip; 
        import flash.events.Event;  
        import flash.events.MouseEvent;  
    
        import fl.transitions.Tween;  
        import fl.transitions.TweenEvent;  
        import fl.transitions.easing.*;  
    
        // to indicate the index of current active circle/square
        var current:int = 0;
        var fadeIn:Tween, fadeOut:Tween; 
    
        var thisCircle:MovieClip; 
        var thisSquare:MovieClip; 
    
    
        var circles:Array = new Array(circle1, circle2, circle3, circle4); 
        var squares:Array = new Array(square1, square2, square3, square4); 
    
    
        for(var i:Number = 0; i < circles.length; i++) 
        { 
        thisCircle = circles[i]; 
        thisCircle.buttonMode = true; 
        thisCircle.id = i; 
        thisCircle.addEventListener(MouseEvent.CLICK, doFadeIn); 
    
        // keep the first square as visible
        if(i != current){
            thisSquare = squares[i]; 
            thisSquare.alpha = 0; 
        }
        } 
    
        function doFadeIn(e:MouseEvent):void  
        { 
        // if our button is the active one, exit
        if(current == e.currentTarget.id) return;
    
        // fade out current square
        fadeOut = new Tween(squares[current], "alpha", None.easeNone, 1, 0, 2.5, true);     
    
        // fade in the new active square
        fadeIn = new Tween(squares[e.currentTarget.id], "alpha", None.easeNone, 0, 1, 2.5, true);  
    
        current = e.currentTarget.id;
        }
    Last edited by kite111; 12-21-2014 at 03:22 AM.

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    You will need to send the data to the php file and then process it to your database (that bit you say you can do ).

    OK, with the structure you already have, add a clip named "submitButton" and a dynamic textField called "responseText".

    flash code
    PHP Code:
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    import fl.transitions.Tween;
    import fl.transitions.TweenEvent;
    import fl.transitions.easing.*;

    import flash.net.URLRequest;
    import flash.net.URLVariables;
    import flash.net.URLRequestMethod;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;

    // to indicate the index of current active circle/square
    var current:int 0;
    var 
    fadeIn:Tween,fadeOut:Tween;

    var 
    thisCircle:MovieClip;
    var 
    thisSquare:MovieClip;

    var 
    circles:Array = new Array(circle1,circle2,circle3,circle4);
    var 
    squares:Array = new Array(square1,square2,square3,square4);
    var 
    choices:Array = new Array("Chocolate","Vanilla","Mocha","Caramel");


    // ** stuff for send and load vars
    var phpPath:String "phpfile.php";

    var 
    chosenType:String;
    var 
    sendVars:URLVariables;
    var 
    sendRequest:URLRequest;
    var 
    sendLoader:URLLoader;


    for (var 
    i:Number 0circles.lengthi++)
    {
        
    thisCircle circles[i];
        
    thisCircle.buttonMode true;
        
    thisCircle.id i;
        
    thisCircle.value choices[i];
        
    thisCircle.addEventListener(MouseEvent.CLICKdoFadeIn);

        
    // keep the first square as visible;
        
    if (!= current)
        {
            
    thisSquare squares[i];
            
    thisSquare.alpha 0;
        }
    }

    function 
    doFadeIn(e:MouseEvent):void
    {
        
    chosenType e.currentTarget.value;
        
    trace(chosenType);

        
    // if our button is the active one, exit
        
    if (current == e.currentTarget.id)
        {
            return;
        }

        
    // fade out current square
        
    fadeOut = new Tween(squares[current],"alpha",None.easeNone,1,0,2.5,true);

        
    // fade in the new active square
        
    fadeIn = new Tween(squares[e.currentTarget.id],"alpha",None.easeNone,0,1,2.5,true);

        
    current e.currentTarget.id;
    }

    submitButton.buttonMode true;
    submitButton.addEventListener(MouseEvent.CLICKsendSelected);

    function 
    sendSelected(e:MouseEvent):void
    {
        if (
    chosenType)
        {
            
    sendLoadData();
            
    responseText.text "You have chosen " chosenType;
            
    trace("Please select a flavour");
        }
        else
        {
            
    responseText.text "Please select a flavour";
            
    trace("Please select a flavour");
        }
    }

    function 
    sendLoadData():void
    {
        
    sendVars = new URLVariables();
        
    sendVars.choice chosenType;

        
    sendRequest = new URLRequest(phpPath);
        
    sendRequest.method URLRequestMethod.POST;
        
    sendRequest.data sendVars;

        
    sendLoader = new URLLoader();
        
    sendLoader.dataFormat URLLoaderDataFormat.VARIABLES;
        
    sendLoader.addEventListener(Event.COMPLETE,sendSuccess);
        
    sendLoader.addEventListener(IOErrorEvent.IO_ERRORsendFailed);
        
    sendLoader.load(sendRequest);
    }

    function 
    sendSuccess(e:Event):void
    {
        
    responseText.text e.target.data.phpReply;
        
    sendLoader.removeEventListener(Event.COMPLETE,sendSuccess);
        
    sendLoader.removeEventListener(IOErrorEvent.IO_ERRORsendFailed);
    }

    function 
    sendFailed(e:Event):void
    {
        
    sendLoader.removeEventListener(IOErrorEvent.IO_ERRORsendFailed);
        
    trace("Something went wrong!!!");

    php code -( phpfile.php ) in this example
    PHP Code:
    <?php

    //Variables received from ActionScript
    $userChoice $_POST['choice'];

    //Check sent data then spit out reply.
    if ($userChoice != "")
    {
        
    $replyMessage "You sent ".$userChoice." to the php file";
        
    $sendReply "phpReply=";
        
    $sendReply .= rawurlencode($replyMessage);
        echo 
    $sendReply;
        
    // do you mysql stuff etc etc...
    }
    else
    {
        
    $replyMessage "You never sent anything to the php file";
        
    $sendReply "phpReply=";
        
    $sendReply .= rawurlencode($replyMessage);
        echo 
    $sendReply;
    }

    ?>
    you will need to use your home server (wamp, xamp) or put it online to test it properly as it will not work directly inside of flash gui.

    Of course all names and vars can be changed to whatever you desire once you have it working and feel competent with it all.

  3. #3
    Member
    Join Date
    Nov 2014
    Posts
    34
    Ok so I fixed the structure.Hope you can guide me sir.
    It now looks like this:

    new.png
    currrent AS:
    PHP Code:
    import flash.display.MovieClip
    import flash.events.Event;  
    import flash.events.MouseEvent;  

    import fl.transitions.Tween;  
    import fl.transitions.TweenEvent;  
    import fl.transitions.easing.*;  

    // to indicate the index of current active movieclip
    var currentCake:int 0;
    var 
    currentFrost:int 0;
    var 
    currentTopping:int 0;
    var 
    fadeIn:TweenfadeOut:Tween

    var 
    thisCake:MovieClip// "button"/mc pink circle on the right
    var thisBase:MovieClip// base mismo ng cake on the left
    var thisFrost:MovieClip// "button"/mc pink circle on the right
    var thisFrosting:MovieClip// frosting mismo ng cake on the left
    var thisTop:MovieClip// "button"/mc pink circle on the right
    var thisTopping:MovieClip// toppings on the left
    var thisNone:MovieClip// "button"/mc pink circle on the right


    var cakebtn:Array = new Array(chocobvanillabmochabcaramelb); // pink circle
    var cake:Array = new Array(bChocobVanillabMochabCaramel);  //the cake mc on the left
    var frostbtn:Array = new Array(chocofvanillafbubblegumfstrawberryf); // pink circle
    var frosting:Array = new Array(fChocofVanillafBubblegumfStrawberry);  //the cake mc on the left
    var toppingbtn:Array = new Array(chocotwhitechocotblacknwhitetchilitnonet); /pink circle
    var topping:Array = new Array(tChocotWhiteChocotBlackNWhitetChilitNone);  //the toppings mc on the left


    for(var i:Number 0cakebtn.lengthi++) 

        
    thisCake cakebtn[i]; 
        
    thisCake.buttonMode true
        
    thisCake.id i
        
    thisCake.addEventListener(MouseEvent.CLICKdoFadeIn); 

        
    // keep the first cake base as visible
        
    if(!= currentCake){
            
    thisBase cake[i]; 
            
    thisBase.alpha 0
        }


    function 
    doFadeIn(e:MouseEvent):void  

        
    // if our button is the active one, exit
        
    if(currentCake == e.currentTarget.id) return;

        
    // fade out current cake
        
    fadeOut = new Tween(cake[currentCake], "alpha"None.easeNone102.5true);     

        
    // fade in the new active cake
        
    fadeIn = new Tween(cake[e.currentTarget.id], "alpha"None.easeNone012.5true);  

        
    currentCake e.currentTarget.id;
    }

    //-------------------------------FROSTING------------------------------------//

    for(var j:Number 0cakebtn.lengthj++) 

        
    thisFrost frostbtn[j]; 
        
    thisFrost.buttonMode true
        
    thisFrost.id j
        
    thisFrost.addEventListener(MouseEvent.CLICKdoFadeInFrost); 

        
    // keep the first frosting as visible
        
    if(!= currentFrost){
            
    thisFrosting frosting[j]; 
            
    thisFrosting.alpha 0
        }


    function 
    doFadeInFrost(e:MouseEvent):void  

        
    // if our button is the active one, exit
        
    if(currentFrost == e.currentTarget.id) return;

        
    // fade out current frosting
        
    fadeOut = new Tween(frosting[currentFrost], "alpha"None.easeNone102.5true);     

        
    // fade in the new active frosting
        
    fadeIn = new Tween(frosting[e.currentTarget.id], "alpha"None.easeNone012.5true);  

        
    currentFrost e.currentTarget.id;
    }

    //-------------------------------TOPPINGS------------------------------------//

    for(var k:Number 0toppingbtn.lengthk++) 

        
    thisTop toppingbtn[k]; 
        
    thisTop.buttonMode true
        
    thisTop.id k
        
    thisTop.addEventListener(MouseEvent.CLICKdoFadeInTopping); 

        
    // keep the first topping as visible
        
    if(!= currentTopping){
            
    thisTopping topping[k]; 
            
    thisTopping.alpha 0
        }


    function 
    doFadeInTopping(e:MouseEvent):void  

        
    // if our button is the active one, exit
        
    if(currentTopping == e.currentTarget.id) return;

        
    // fade out current topping
        
    fadeOut = new Tween(topping[currentTopping], "alpha"None.easeNone102.5true);     

        
    // fade in the new active topping
        
    fadeIn = new Tween(topping[e.currentTarget.id], "alpha"None.easeNone012.5true);  

        
    currentTopping e.currentTarget.id;

    all are MCs. I added a button btnSubmit. What do I do next?
    Last edited by kite111; 12-22-2014 at 10:19 AM.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    I kind of gave you the way to do it in the last post, I am definitely not going to try and recreate your file amd graphics, I don't have that time.

  5. #5
    Member
    Join Date
    Nov 2014
    Posts
    34
    Hi.

    I'm sorry, I wasn't making you do it from the start. Descriptions will do.

    Anyway, I'm going back to basic, just the circle and squares. I'll try it first to understand the whole process on how to do it.

    I added the dynamic text and submit button.Did I do it right?

    Untitled.png

    Though I don't quite understand this part with the database stuff.
    PHP Code:
    <?php

    //Variables received from ActionScript
    $userChoice $_POST['choice'];

    //Check sent data then spit out reply.
    if ($userChoice != "")
    {
        
    $replyMessage "You sent ".$userChoice." to the php file";
        
    $sendReply "phpReply=";
        
    $sendReply .= rawurlencode($replyMessage);
        echo 
    $sendReply;
        
    // do you mysql stuff etc etc...
    }
    else
    {
        
    $replyMessage "You never sent anything to the php file";
        
    $sendReply "phpReply=";
        
    $sendReply .= rawurlencode($replyMessage);
        echo 
    $sendReply;
    }

    ?>

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    That part is not mysql, that part is simple php checking for what data flash sent to it and then responding accordingly.
    The var $userChoice ( derived from the flash var of sendVars.choice = chosenType;)
    is the part you would send to the database if all criteria was met.

    As I say before, you ned to have a home server installed ( wamp, xamp, or other )or actually put it online somewhere for you to test this stuff properly, it simply will not work correctly inside of flash ( the php and mysql part that is ).

  7. #7
    Member
    Join Date
    Nov 2014
    Posts
    34
    I put in the phpfile page. It's giving this error and nothing happens aside from the tweening stuff.

    Untitled2.png

    this is line 32
    $userChoice = $_POST['choice'];

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    I'm not sure what you might have incorrectly done.

    Attachment 75201

    actuall you need to make the html file and only target the php from within flash.

    try opening localhost/MCC/cake.swf ( or whatever you called the swf ) and see the outcome, the php file need never be accessed directly through the browser

    check the browser address of my image
    Last edited by fruitbeard; 12-23-2014 at 02:34 AM.

  9. #9
    Member
    Join Date
    Nov 2014
    Posts
    34
    Thanks. The error is gone. The problem was i put everything on phpfile.php. I separated the swf in a html file. But i don't know why the text isn't appearing when i click submit. >.<

Tags for this Thread

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