dcsimg
A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [Help][F8] Sending to php with sendAndLoad

  1. #1

    [Help][F8] Sending to php with sendAndLoad

    Hi,
    I'm creating my own highscores table for a flash game.
    I can send the scores with:

    Code:
    getURL("http://www.mysite.com/new_score.php", "", "POST");
    it works, but I don't like that it opens a new window as soon as I click "submit".

    So I read that using sendAndLoad would be best.
    But, I'm unsure of how it works. It seems i need to create an object. So I try:

    Code:
    var send_mc = this.createEmptyMovieClip("send_mc",this.swapDepths(1));
    
    send_mc.pass = _root.pass; 
    send_mc.name = _root.name;
    send_mc.score = _root.score;
    
    send_mc.sendAndLoad("http://www.mysite.com/new_score.php", "", "POST");
    But the score is not sent...
    Do I need to modify something inside the .php to be able to work with sendAndLoad instead of getURL?

    Thanks!

    P.S. If it is of any help, I used this tutorial as a reference: http://www.flashkit.com/tutorials/Ga...-785/index.php so you can have a good idea of what my php files are like.
    Last edited by SnailsAnimation; 10-15-2008 at 02:19 AM.

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    sendAndLoad isnt a method defined for a movieclip... its defined for LoadVars ...use the following instead:

    PHP Code:
    var send_mc:LoadVars = new LoadVars(); 

    send_mc.pass _root.pass
    send_mc.name _root.name;
    send_mc.score _root.score;

    send_mc.sendAndLoad("http://www.mysite.com/new_score.php"send_mc"POST"); 
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    Thank you, it worked right away!

  4. #4
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    you're welcome
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  5. #5
    Hmm... I have 2 other problems:

    1. It's seems that something goes wrong when I try to send a score more than once (like after each game) without closing the swf.
    However, if I close the swf between each time the score is sent there is no problem.

    What it does:
    -The first time the name and score is ok.
    -The other time only the name is sent right (the score is 0).

    2. If you enter the same name as another one already in the highscores, it replaces with the new score. (instead of having 2 entries with the same names but with different scores)
    If 2 different entries have the same score there is no problem.

  6. #6
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    very hard to help without seeing more of your code - where it updates the highscore list, score etc..

    post back the relevant code and hopefully we can take it from there..
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  7. #7
    For the flash part, the code is the same as above.

    Here is the php code in the new_score.php file (the one that sendAndLoad sends to) as you can see I used code from a tutorial found here on flashkit, because php and sql is all new to me:

    PHP Code:
    <?
    //****Made by: Jeroen den Haan***Alias's: jeroen84 / Jer'ul****
    include_once ("_data.php");

    $conn mysql_connect($db_host,$db_user,$db_pass);
    mysql_select_db($db_name,$conn);

    if (
    $pass==$add_pass) {

    if (
    $u_user==1) {

        
    $sql3 "SELECT name FROM $db_table ORDER BY name";
        
    $result3 mysql_query($sql3);
        while(
    $r mysql_fetch_object($result3)) 
        {
            
    $tmp "{$r->name}";
            if (
    $name==$tmp) {
                
    $n_exist=1;

            }
        }
        if (
    $n_exist==1) {
            
    $sql1 "UPDATE $db_table SET score='$score' WHERE name=\"$name\"";
            
    $result1 mysql_query($sql1);
        } else {
            
    $sql1 "INSERT INTO $db_table (name,score) VALUES (\"$name\",\"$score\")";
            
    $result1 mysql_query($sql1);
        }

    } else {
    $sql1 "INSERT INTO $db_table (name,score) VALUES (\"$name\",\"$score\")";
    $result1 mysql_query($sql1);
    }

    $sql2 "SELECT id FROM $db_table ORDER BY score DESC";
    $result2 mysql_query($sql2);

    $num 1;
    while(
    $r mysql_fetch_object($result2))
    {
    $result mysql_db_query($db_name,"SELECT * from $db_table WHERE id='{$r->id}'");
    $resultArray mysql_fetch_array($result);
    $did $resultArray["id"];
    $name $resultArray["name"];
    $score $resultArray["score"];
    if (
    $num>$sec_size) {
        
    $sql3 "DELETE FROM $db_table WHERE id='$did'";
        
    $result3 mysql_query($sql3);
    }
    $num++;
    }
    }

    mysql_close ($conn);
    ?>

    I'm not sure if this can help, but this is the code for the sql part (the highscore table):
    Code:
    $sql1 = "CREATE TABLE $db_table (
    	id int(4) NOT NULL auto_increment, 
    	name varchar(12) DEFAULT 'none' NOT NULL, 
    	score int(2) DEFAULT '0' NOT NULL, 
    	UNIQUE id (id)
    	)";

    Please tell if you need more info.

  8. #8
    Ok both problems have been solved.
    For the first problem:
    to test the scores I made a small flash where you manually enter the name AND score with input texfields. Then you press submit and go check if it worked. But it seems that for the name variable it was ok to enter it manually (like in real games), but not for the score. So if the score is not sent from an input textfield it works! Took a while to figure it out..

    For the second problem:
    In the tutorial file called _data.php the u_user variable was set to 1. And if it was 1 in meant that there couldn't be two entries with the same name. If two different scores where sent, but with the exact name it would simply update the last score. So by changing it to 0 it will allow to have 2 entries with the same name.

  9. #9
    Now I have another problem:

    When I send a score from inside Flash, it works. The score is sent to my php file on my website with no problem.
    But, if I try the .swf in my desktop for example, it fails to send the scores. And if I upload the file on my website too.

    Is that some kind of security issue?

    In publish settings the local playback security is set to "access network only".
    I tried:
    System.security.allowDomain("mysite.com");

    But it doesn't seem to change anything. Is anyone familiar with this problem?

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