Hi!
I've been learning flash as3 lately, and I want to redo my website in flash, using the same db, so that they can both be viewed by the users.
I found a cool tutorial on the web for a PHP/mySQL/AS3 guestbook, but I can't get the display to work. Actually, the values don't even get added to the db table (I used my existing guestbook table, called guestbookn, with a little more than 100 entries, so it's not empty).
The php script is as follow : (I modified it a little for my needs, but the original version didn't work either . I could get the values inserted into the DB, but no display)
PHP Code:
<?
// here the DB connection parameters (not very interesting, and I know the file DOES connect to the DB, so the problem doesn't come from here)
$connection = mysql_connect("$hostname" , "$user" , "$pass");
mysql_select_db($dbase , $connection) or die (mysql_error());
if ($_POST['comType'] == "parseComment") {
$name = $_POST['user_name'];
$email = $_POST['user_email'];
$sitename = $_POST['user_siteName'];
$siteurl = $_POST['user_siteURL'];
$comment = $_POST['user_message'];
// Filter user input a little bit further using PHP if you allow more characters than I do in the Flash input text field
$name = mysql_real_escape_string($name);
$email = mysql_real_escape_string($email);
$sitename = mysql_real_escape_string($sitename);
$siteurl = mysql_real_escape_string($siteurl);
$post = mysql_real_escape_string($comment);
// uncomment this line below to preserve line breaks, paragraphs and such in the comment text
$post = nl2br(htmlspecialchars($comment));
// Add to DB
$sql = mysql_query("INSERT INTO guestbookn (name, email, sitename, siteurl, date, comments)
VALUES('$name', '$email', '$sitename', '$siteurl', now(),'$comment')")
or die (mysql_error());
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Assemble body and send back to flash showing their new comment or entry
$body = "";
$sql = mysql_query("SELECT * FROM guestbookn ORDER BY post_date DESC");
while($row = mysql_fetch_array($sql)) {
$id = $row["id"];
$name = $row["name"];
$email = $row["email"]
$post_date = $row["date"];
$comment = $row["comments"];
$sitename = $row["sitename"];
$siteurl = $row["siteurl"];
$n_post_body = str_replace("<br />", "", $n_post_body); // Use in case you get too many line breaks when preserving breaks
$comment = stripslashes($comment);
$name = eregi_replace("'", "'", $name);
$email = eregi_replace("'", "'", $email);
$sitename = eregi_replace("'", "'", $sitename);
$siteurl = eregi_replace("'", "'", $siteurl);
$comment = eregi_replace("'", "'", $comment);
// Decode HTML entities if storing comments that preserve line breaks and such
$n_post_body = html_entity_decode($n_post_body); // Uncomment to use
$post_date = strftime("%b %d, %y", strtotime($post_date));
if ( ($siteurl == '' AND $sitename == '') )
{
$body .= '<u><b><font color="#790000">' . $name . '</font> | <font color="#9B9B9B">' . $post_date . '</font></b></u>
<br />
'.$comment.'
<br />
<br />
';
}
elseif ( $siteurl == '' AND $sitename != '' )
{
$body .= '<u><b><font color="#790000">' . $name . '</font> | <font color="#9B9B9B">' . $sitename . '</font> | <font color="#9B9B9B">' . $post_date . '</font></b></u>
<br />
'.$comment.'
<br />
<br />
';
}
elseif ( ($siteurl != '' AND $sitename == '') )
{
$body .= '<u><b><font color="#790000">' . $name . '</font> | <a href="'. $siteurl.'" target="_blank"><font color="#9B9B9B">' . $siturl . '</font></a> | <font color="#9B9B9B">' . $post_date . '</font></b></u>
<br />
'.$comment.'
<br />
<br />
';
}
else
{
$body .= '<u><b><font color="#790000">' . $name . '</font> | <a href="'. $siteurl.'" target="_blank"><font color="#9B9B9B">' . $sitename . '</font></a> | <font color="#9B9B9B">' . $post_date . '</font></b></u>
<br />
'.$comment.'
<br />
<br />
';
}
}
mysql_free_result($sql);
mysql_close();
// Echo into flash
echo "return_msg=Entry has been added successfully $name, thanks!&returnBody=$body";
exit();
} // close first if for post
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
::::::::::Script Written By: Adam Khoury @ www.developphp.com:::::::::::::
:::::::::If you find www.developphp.com tutorials helpful or handy:::::::::::::
:::::::::::please link to it wherever possible to help others find it::::::::::::::::
*/
// Second part of the script is below, it simply requests all entries for initial display of the guestbook entries
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if ($_POST['comType'] == "requestEntries") {
$body = "";
$sql = mysql_query("SELECT * FROM guestbookn ORDER BY post_date DESC");
while($row = mysql_fetch_array($sql)) {
$id = $row["id"];
$name = $row["name"];
$email = $row["email"];
$sitename = $row["sitename"];
$siteurl = $row["siteurl"];
$post_date = $row["date"];
$comment = $row["comments"];
$comment = stripslashes($comment);
// Decode HTML entities if storing comments that preserve line breaks and such
$n_post_body = html_entity_decode($n_post_body); // Uncomment to use
$post_date = strftime("%b %d, %y", strtotime($post_date));
if ( ($siteurl == '' AND $sitename == '') )
{
$body .= '<u><b><font color="#790000">' . $name . '</font> | <font color="#9B9B9B">' . $post_date . '</font></b></u>
<br />
'.$comment.'
<br />
<br />
';
}
elseif ( $siteurl == '' AND $sitename != '' )
{
$body .= '<u><b><font color="#790000">' . $name . '</font> | <font color="#9B9B9B">' . $sitename . '</font> | <font color="#9B9B9B">' . $post_date . '</font></b></u>
<br />
'.$comment.'
<br />
<br />
';
}
elseif ( ($siteurl != '' AND $sitename == '') )
{
$body .= '<u><b><font color="#790000">' . $name . '</font> | <a href="'. $siteurl.'" target="_blank"><font color="#9B9B9B">' . $siturl . '</font></a> | <font color="#9B9B9B">' . $post_date . '</font></b></u>
<br />
'.$comment.'
<br />
<br />
';
}
else
{
$body .= '<u><b><font color="#790000">' . $name . '</font> | <a href="'. $siteurl.'" target="_blank"><font color="#9B9B9B">' . $sitename . '</font></a> | <font color="#9B9B9B">' . $post_date . '</font></b></u>
<br />
'.$comment.'
<br />
<br />
';
}
}
mysql_free_result($sql);
mysql_close();
echo "returnBody=$body";
exit();
} // close first if for post
?>
My AS3 in my flash file :
The sending data to php part :
PHP Code:
msg_txt.restrict="^*#<>/";
name_txt.restrict="^*#<>/";
email_txt.restrict="^*#<>/";
siteName_txt.restrict="^*#<>/";
siteURL_txt.restrict="^*#<>";
// textformat pour les erreurs et les messages d'alerte
var errorsFormat:TextFormat=new TextFormat();
errorsFormat.color=0x0000FF;
// cacher le processing
processing_mc.visible=false;
// assigner un nom de variables pour l'URLVariable.
var variables:URLVariables=new URLVariables();
// varSend variable
var varSend:URLRequest=new URLRequest("guestbookParsemoi.php");
varSend.method=URLRequestMethod.POST;
varSend.data=variables;
// varLoader
var varLoader:URLLoader=new URLLoader();
varLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
// handler
function completeHandler(event:Event):void {
processing_mc.visible=false;
// clear textfields
msg_txt.restrict="";
name_txt.restrict="";
email_txt.restrict="";
siteName_txt.restrict="";
siteURL_txt.restrict="";
// charger la réponse du php
status_txt.text=event.target.data.return_msg;
gbOutput_txt.condenseWhite=true;
gbOutput_txt.htmlText="" + event.target.data.returnBody;
}
// event listener sur le bouton submit :
submit_btn.addEventListener(MouseEvent.MOUSE_DOWN, ValidateAndSend);
// fonction de validation et d'envoi :
function ValidateAndSend(event:MouseEvent):void {
// validate form fields :
if (!name_txt.length) {
status_txt.text="Veuillez inscrire votre nom.";
status_txt.setTextFormat(errorsFormat);
}
else if (!email_txt.length) {
status_txt.text="Veuillez inscrire votre email (il ne sera pas publié).";
status_txt.setTextFormat(errorsFormat);
}
else if (!msg_txt.length) {
status_txt.text="Veuillez écrire un message.";
status_txt.setTextFormat(errorsFormat);
}
else {
// tout est bon, donc on envoie :
processing_mc.visible=true;
// préparer les variables pour l'envoi :
variables.comType="parseComment";
variables.user_name=name_txt.text;
variables.user_siteURL=siteURL_txt.text;
variables.user_siteName=siteName_txt.text;
variables.user_email=email_txt.text;
variables.user_message=msg_txt.text;
// envoyer les données au php :
varLoader.load(varSend);
// un message pour dire qu'il est en train de faire la connexion :
status_txt.text="En attente de la connexion au serveur.";
}
}