Groups | Blog | Home
all groups > flash data integration > december 2005 >

flash data integration : sendandload to php problem


connoru
12/15/2005 3:13:56 PM
I thought this should be relatively straightforward but ....

I want to send two variables from a swf to a php script.
The php script then inserts them into a table row in a mySQL database.
I can do this with the loadVars send() function. I have this working. However,
this opens a new browser window.
I want the php script to run in the background (i.e. no new window).
I have tried the sendandload() function with various combinations of GET's and
POST's but no joy.
It is sending the variablesand the load returns success but I'm not getting
updates in the table.
It seems that the php script is not running? I'm fairly new to php. Does it
need to be in a browser to execute?
Heres the code

//AS

_global.sendNames = function(First, Last) {
var result_lv:LoadVars = new LoadVars();
result_lv.onLoad = function(success:Boolean) {
if (success) {
trace("LOAD Success");
trace(result_lv.PHPresult);
} else {
trace("LOAD Fail");
trace(result_lv.PHPresult);
}
};
var send_lv:LoadVars = new LoadVars();
send_lv.firstName = First;
send_lv.lastName = Last;
trace("SENDING NAMES");
trace(First);
trace(Last);
send_lv.sendAndLoad("http://localhost/experiment/usersUpdate.php",
result_lv, "POST");
}

//PHP
<?
$host = "localhost";
$user = "root";
$pass = "28phaedruS";

$firstName = $_GET['firstName'];
$lastName = $_GET['lastName'];

$db="results";
$link = mysql_connect($host, $user, $pass);
if (! $link)

$PHPresult="Couldn't connect to MySQL";
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());

mysql_query ("INSERT INTO users(firstName,lastName) VALUES
('$firstName','$lastName')");

echo("firstName=".$firstName."<br />");
echo("lastName=".$lastName."<br />");
mysql_close($link);
?>
Any Ideas?

Motion Maker
12/15/2005 9:05:02 PM
1. Only PHP in the file. No html in other words.
2. No whitespace (sp cr tab) before the first php tag <?php or after the
last php tag ?>
3. echo back URL encoded variable pairs

var1=value&var2=value&var3=value:

echo "firstName=" . $firstName;
echo "&lastName=" . $lastName;

4. If you are using just the loadVars send method, then the PHP should not
echo any values back. Otherwise you need to use the sendAndLoad method and
send back URL encoded variable pairs.


5. You really do not want to use die and other PHP functions that result in
sending text. Rather build PHP code that captures the errors and if needed
return URL vars back to Flash for UI feedback.

echo "&success=" . $success ; // figured elsewhere
echo "&returnMessage=" . "Couldn't open $db: " . mysql_error() ; // If you
need to see in Flash for debugging


Here is a basic send and return with loadVars and PHP I use in training
courses.

http://www.hosfordusa.com/ClickSystems/courses/flash/examples/LoadVars/Ex01/LoadVarsEx01.php

--
Lon Hosford
www.lonhosford.com
May many happy bits flow your way!
[quoted text, click to view]
I thought this should be relatively straightforward but ....

I want to send two variables from a swf to a php script.
The php script then inserts them into a table row in a mySQL database.
I can do this with the loadVars send() function. I have this working.
However,
this opens a new browser window.
I want the php script to run in the background (i.e. no new window).
I have tried the sendandload() function with various combinations of GET's
and
POST's but no joy.
It is sending the variablesand the load returns success but I'm not getting
updates in the table.
It seems that the php script is not running? I'm fairly new to php. Does it
need to be in a browser to execute?
Heres the code

//AS

_global.sendNames = function(First, Last) {
var result_lv:LoadVars = new LoadVars();
result_lv.onLoad = function(success:Boolean) {
if (success) {
trace("LOAD Success");
trace(result_lv.PHPresult);
} else {
trace("LOAD Fail");
trace(result_lv.PHPresult);
}
};
var send_lv:LoadVars = new LoadVars();
send_lv.firstName = First;
send_lv.lastName = Last;
trace("SENDING NAMES");
trace(First);
trace(Last);
send_lv.sendAndLoad("http://localhost/experiment/usersUpdate.php",
result_lv, "POST");
}

//PHP
<?
$host = "localhost";
$user = "root";
$pass = "28phaedruS";

$firstName = $_GET['firstName'];
$lastName = $_GET['lastName'];

$db="results";
$link = mysql_connect($host, $user, $pass);
if (! $link)

$PHPresult="Couldn't connect to MySQL";
mysql_select_db($db , $link)
or die("Couldn't open $db: ".mysql_error());

mysql_query ("INSERT INTO users(firstName,lastName) VALUES
('$firstName','$lastName')");

echo("firstName=".$firstName."<br />");
echo("lastName=".$lastName."<br />");
mysql_close($link);
?>
Any Ideas?

flash-hero
12/17/2005 10:26:34 PM
AddThis Social Bookmark Button