PDA

View Full Version : passing info from flash to PHP


nyghtrunner
06-29-2007, 06:36 PM
Hi all,

So I've been using actionscript for a while now, and am just getting into PHP. he biggest reason is that I need things to be able to post to a dbase from flash, and then to allow flash to retrieve things from the dbase. The retrieving isn't really a problem, but the sending is a real challenge since I don't really know PHP at all. Anyways, here a sample of some code I'm trying to use to have my flash file send something, have PHP look at it and process it, and then send it right back to flash (no dbase interaction at this point).


///////////////////////////Function that calls and passes info to the PHP and then gathers return data//////////////////////
tester.buttonHolder_mc.s1_mc.onRelease = function() {

var input1 = tester.textHolder_mc.nameInput.text;
var input2 = tester.textHolder_mc.postInput.text;

tester.textHolder_mc.nameInput.text = "type name here";
tester.textHolder_mc.nameInput.setTextFormat(forma tInput_fmt);
tester.textHolder_mc.postInput.text = "type submission here";
tester.textHolder_mc.postInput.setTextFormat(forma tInput_fmt);

var poster = new LoadVars();

poster.nameText = input1;
poster.postText = input2;

poster.onLoad = addPost();

poster.sendAndLoad("tester.php",poster,"POST");
}


function addPost() {
tester.textHolder_mc.postOutput.text = poster.soapOut;
tester.textHolder_mc.postOutput.setTextFormat(form atOutput_fmt);
}



I know this is pretty much working as I need it to internally, but here's what I don't know... This is the PHP starter I have. As I said, I'm new to the PHP thing, so it might be totally wrong, but I really just want it to accept the data and spit it right back into flash.



<?php
// Incoming data via Post
$nameText = $HTTP_POST_VARS[nameText];
$postText = $HTTP_POST_VARS[postText];

// Include the NuSoap Class's.
require_once('nusoap.php');

// Add necessary Parameters.
$parameters = array(
"nametext" =>$nameText,
"posttext" =>$postText
);

$result = "$nameText+$postText";

// Print the result.
print "&soapOut=$result&";

?>



Assume I'm stupid about PHP. I can look at it and see what it's doing for the most part, as I have been programming for a short while now, but I am not at all familiar enough with the syntax or the structure to be able to write it... :mad:

Anyways, any help would be appreciated!

Stephen

Scuba_Steve
07-06-2007, 05:05 PM
change

$nameText = $HTTP_POST_VARS[nameText];
$postText = $HTTP_POST_VARS[postText];

to

$nameText = $_POST['nameText'];
$postText = $_POST['postText']; (note the apostrophes)

oh, and if it's just your syntax you're concerned about, the best way i know to "check" your questionable php is to add print "i'm working correctly"; at the end of your php. then navigate to your script w/ a browser. if you don't see "i'm working correctly", it's not working correctly. ;)

nyghtrunner
07-06-2007, 06:04 PM
Thanks Scuba,

I was actually able to get it working fairly well after a lot of trial and error. Here's the code I came up with, and I went ahead and got it writing to the database and returning the dbase value instead of just regurgitating what I sent the PHP file.:)


<?php
// Incoming data via Post
$nameText = $_POST['nameText'];
$postText = $_POST['postText'];

// Include the NuSoap Class's.
require_once('nusoap.php');

// Add necessary Parameters.
$parameters = array(
"nametext"=>$nameText,
"posttext"=>$postText
);

$link= @mysql_connect('localhost', 'root', 'password');
if(!$link) {
$ribbit = mysql_error();
echo "&soapOut=$ribbit&";
exit();
}

mysql_select_db('throwawaytesting',$link);

$query = "INSERT INTO myTester SET
name='$nameText',
quote='$postText',
postDate=CURRENT_TIMESTAMP";

if (!@mysql_query($query)){
$ribbit = mysql_error();
echo "&soapOut=$ribbit&";
exit();
};

$result = $nameText.' wrote: '.$postText;

// Print the result.
print "&soapOut=$result&";

?>


Not sure if it's the cleanest, but it's working pretty well for what I need. Also, I figured out a way to bootleg a trace, just setting up a "send" in flash that goes to the same PHP page, but opens up in an _blank (shiny new page). That, along with echo and/or print was able to tell me if my vars were passing and being processed correctly! ;)

Stephen

Scuba_Steve
07-09-2007, 01:23 PM
yeah i wish there was an app for writing/debugging php that was as helpful as the Flash IDE. :)