Solved

Variables coming through 'Undefined' in Flash/php email form

Posted on 2011-02-27
19
699 Views
Last Modified: 2012-05-11
Hi-  

I'm trying to teach myself how to make an email form work in flash.  I was following this tutorial: http://www.sephiroth.it/tutorials/flashPHP/email/page002.php.  

So far, I created a form at http://www.burlingtonglass.net/ under contact (Frame 40).  In the tutorial, the Send button is actually a movie clip with a button inside.  I named the movie clip instance sendMC.

In the button action I have:
on (release) {
    //    Now import the variables we
    //    need to send in this movie clip
    sender_mail = _root.Semail.text
    sender_name = _root.Sname.text
    sender_message = _root.Smessage.text
    //    all the vars we just imported
    //    will be sent via POST method now
    loadVariables("sendmail.php",this,"POST");
    //    and when receives the answer from
    //    the server...
    this.onData = function()
    {
        //    ok, next frame
        _root.nextFrame();
        if(this.output=='sent')
        {
            // in case of success
gotoAndStop("thankyou");
        } else {
            //    else
       gotoAndStop("error");
        }
    }
}

(I also tried putting that in the movie clip action, but that didn't work either)

I double checked that my inputs have the names Sname, Semail and Smessage.  Here is the script sendmail.php:


<?
if(!empty($_POST['sender_mail'])
    || !empty($_POST['sender_message'])
    || !empty($_POST['sender_name']))
{
    $to = "xanabobana@yahoo.com";
    $s_name = $_POST['sender_name'];
    $s_mail = $_POST['sender_mail'];
    $subject = "BurlingtonGlass.net Online Form Submission";
    $body = stripslashes($_POST['sender_message']);
    $body .= "\n\n---------------------------\n";
    $body .= "Mail sent by: $s_name <$s_mail>\n";
    $header = "From: $s_name <$s_mail>\n";
    $header .= "Reply-To: $s_name <$s_mail>\n";
    $header .= "X-Mailer: PHP/" . phpversion() . "\n";
    $header .= "X-Priority: 1";
    if(@mail($to, $subject, $body, $header))
    {
        echo "output=sent";
    } else {
        echo "output=error";
    }
} else {
    echo "output=error";
}
?>


All of the variables are coming through as undefined & I can't see why not...I'll attach the flash file, you can see the example at http://www.burlingtonglass.net/ click on contact.  

Thanks for your help!
0
Comment
Question by:xanabobana
  • 9
  • 7
19 Comments
 
LVL 6

Expert Comment

by:Tonypeswani
Comment Utility
$ makes a word a variable in the PHP language so you need to take the info posted using the global $_POST function:


PHP Code:
<?PHP

$to = "xxxxxxx@hotmail.com";
$subject = "Tony Sadr Contact Form";
$message .= "Title: " . $_POST['theTitle'];
$message .= "\nName: " . $_POST['theName'];
$message .= "\nEmail: " . $_POST['theEmail'];
$message .= "\n\nMessage: " . $_POST['theMessage'];
$headers = "From: " . $_POST['theEmail'];
$headers .= "\nReply-To: " . $_POST['theEmail'];

mail($to,$subject,$message,$headers);

?>



Thank you
0
 

Author Comment

by:xanabobana
Comment Utility
I'm confused, I have $ on my variables in the php script.  

how is that different from what I have:

$s_name = $_POST['sender_name'];
$s_mail = $_POST['sender_mail'];
$subject = "BurlingtonGlass.net Online Form Submission";
$body = stripslashes($_POST['sender_message']);
$body .= "\n\n---------------------------\n";
$body .= "Mail sent by: $s_name <$s_mail>\n";
0
 
LVL 37

Accepted Solution

by:
CyanBlue earned 500 total points
Comment Utility
You might want to specify what you want to send in Flash...  Please take a look at this tutorial specifically around 3. Sending out an email...
   http://tutorials.flashvacuum.com/index.php?show=Email101

CyanBlue
0
 

Author Comment

by:xanabobana
Comment Utility
I followed this new tutorial, which was pretty much exactly what I already had...but I tried the new code just to see, changed my instance names to match, and am now not even getting an email with undefined variables. now nothing happens at all.

I'm uploading the flash file w/ the new code (from tutorial above) now to:
http://www.burlingtonglass.net/zigmund1.fla
so you can see (its too big to attach)



0
 

Author Comment

by:xanabobana
Comment Utility
changed the php file to match the tutorial's too...
0
 
LVL 37

Expert Comment

by:CyanBlue
Comment Utility
Can you post the updated codes???

CyanBlue
0
 

Author Comment

by:xanabobana
Comment Utility

On scene 40 in the flash file (w/ Sname, Semail and Smessage fields, send_btn button):

stop();

init();

function init()
{

      
      this.Sname.tabIndex = 1;
      this.Semail.tabIndex = 2;
      this.Smessage.tabIndex = 3;
      this.send_btn.tabIndex = 4;
      
      this.Sname.text = "";
      this.Semail.text = "";
      this.Smessage.text = "";
      
      setTextFocus(this.Sname);
}

function setTextFocus(tf)
{
      Selection.setFocus(tf);
}

this.send_btn.onPress = function ()
{
      validateForm();
}


function validateForm()
{
      if ((Sname.text.isset()) && (Sname.text != "Required Field"))
      {
            if ((Semail.text.isset()) && (Semail.text.isEmail()) && (Semail.text != "Required Field"))
            {
                  trace("Semail.text.isEmail() = " + Semail.text.isEmail());
                  if ((Smessage.text.isset()) && (Smessage.text != "Required Field"))
                  {
                        
                        sendEmail();
                  }
                  else
                  {
                        Smessage.text = "Required Field";
                        setTextFocus(Smessage);
                  }
            }
            else
            {
                  Semail.text = "Required Field";
                  setTextFocus(Semail);
            }
      }
      else
      {
            Sname.text = "Required Field";
            setTextFocus(Sname);
      }
}
String.prototype.isset = function ()
{
      if ((this == undefined) || (this == null) || (this == ""))
      {
            return false;
      }
      else
      {
            return true;
      }
}


String.prototype.isEmail = function()
{
      //email address has to have at least 5 chars
      if (this.length < 5)
      {
            return false;
      }
      
      var iChars = "*|,\":<>[]{}`';()&$#%";
      var eLength = this.length;
      
      for (var i = 0; i < eLength; i++)
      {
            if (iChars.indexOf(this.charAt(i)) != -1)
            {
                  trace("Invalid Email Address : Illegal Character in Email Address : -->" + this.charAt(i) + "<--.");
                  return false;
            }
      }
      
      var atIndex = this.lastIndexOf("@");
      if (atIndex < 1 || (atIndex == eLength - 1))
      {
            trace("Invalid Email Address : Email Address must contain @ as at least the second chararcter.");
            return false;
      }
      
      var pIndex = this.lastIndexOf(".");
      if (pIndex < 4 || (pIndex == eLength - 1))
      {
            trace("Invalid Email Address : Email Address must contain at least one . (period) in a valid position");
            return false;
      }
      
      if (atIndex > pIndex)
      {
            this.__error = "Invalid Email Address : Email Address must be in the form of name@domain.domaintype";
            return false;
      }
      
      return true;
};

function sendEmail()
{
      email_lv = new LoadVars();
      email_lv.name = this.Sname.text;
      email_lv.email = this.Semail.text;
      email_lv.message = this.Smessage.text;
      email_lv.onLoad = function (ok)
      {
            if (ok)
            {
                  trace("Email Sent!!!");
                  trace(unescape(this));
                  gotoAndStop("thankyou");
            }
            else
            {
                  trace("Problem sending an Email!!!");
                  info_mc.gotoAndStop("error");
            }
      }
      email_lv.sendAndLoad("sendmail.php", email_lv, "POST");
}



 sendmail.php
0
 
LVL 37

Expert Comment

by:CyanBlue
Comment Utility
Let's do a quick test...  Update these functions in your code...
function validateForm()
{
      trace("Function validateForm");
      
      if ((Sname.text.isset()) && (Sname.text != "Required Field"))
      {
            trace("All fields are filled.");
            if ((Semail.text.isset()) && (Semail.text.isEmail()) && (Semail.text != "Required Field"))
            {
                  trace("Semail.text.isEmail() = " + Semail.text.isEmail());
                  if ((Smessage.text.isset()) && (Smessage.text != "Required Field"))
                  {
                        trace("Calling sendEmail function.");
                        sendEmail();
                  }
                  else
                  {
                        trace("Smessage field is not filled.");
                        Smessage.text = "Required Field";
                        setTextFocus(Smessage);
                  }
            }
            else
            {
                  trace("Semail field is not filled.");
                  Semail.text = "Required Field";
                  setTextFocus(Semail);
            }
      }
      else
      {
             trace("SName field is not filled.");
             Sname.text = "Required Field";
             setTextFocus(Sname);
      }
}

function sendEmail()
{
      trace("Function sendEmail");
      
      email_lv = new LoadVars();
      email_lv.name = this.Sname.text;
      email_lv.email = this.Semail.text;
      email_lv.message = this.Smessage.text;
      trace("email_lv.name = " + email_lv.name);
      trace("email_lv.email = " + email_lv.email);
      trace("email_lv.message = " + email_lv.message);
      email_lv.onLoad = function (ok)
      {
            if (ok)
            {
                  trace("Email Sent!!!");
                  trace(unescape(this));
                  gotoAndStop("thankyou");
            }
            else
            {
                  trace("Problem sending an Email!!!");
                  info_mc.gotoAndStop("error");
            }
      }
      email_lv.sendAndLoad("http://www.domain.com/sendmail.php", email_lv, "GET");
}

Open in new window

Oh, don't forget to update the path to the PHP file in the last line...

Also, replace all of POST in PHP code to GET and test inside the Flash and paste the trace output so that we can see it...

CyanBlue
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:xanabobana
Comment Utility
It says email sent, but it hasn't come through yet.  this was the output:

Function validateForm
All fields are filled.
Semail.text.isEmail() = true
Calling sendEmail function.
Function sendEmail
email_lv.name = xana
email_lv.email = xanabobana@yahoo.com
email_lv.message = test 2
Email Sent!!!
 =&result=1&  
      =&onLoad=[type Function]&message=test 2&email=xanabobana@yahoo.com&name=xana
0
 
LVL 37

Expert Comment

by:CyanBlue
Comment Utility
In that case, it probably is your server that's not doing the mail job right...  Where is your email script???  

CyanBlue
0
 

Author Comment

by:xanabobana
Comment Utility

     
<?php
 
      if ($_POST)
      {
      $mailTo = "xanabobana@yahoo.com";

      $mailSubject = "[Contact] Website Contact from - (" . $_POST['name'] . ")";
      $Header = "MIME-Version: 1.0\r\n";
      $Header .= "Content-type: text/html; charset=iso-8859-1\r\n";
      $Header .= "From: " . $_POST['email'] . "\r\n";

      $output = "<BR>";
      $output .= "From : " . $_POST['name'] . "<BR><BR>";
      $output .= "Email : " . $_POST['email'] . "<BR><BR>";
      $output .= "Message : " . $_POST['message'] . "<BR><BR>";

      $output = nl2br($output);

      if (mail($mailTo, $mailSubject, stripslashes($output), $Header))
      {
      echo("&result=1&");
      }
      else
      {
      echo("&result=2&");
      }
      }

      else
 
      {
 
      echo("This script runs only in Flash!!!");
 
      }

      ?>
0
 
LVL 37

Expert Comment

by:CyanBlue
Comment Utility
I guess you did not read what I said...  ;)
---
Also, replace all of POST in PHP code to GET and test inside the Flash and paste the trace output so that we can see it...
---
To be exact, $_POST ---> $_GET

CyanBlue
0
 

Author Comment

by:xanabobana
Comment Utility
I did that but changed it back since it wasn't working.   it's back to $_GET now
0
 
LVL 37

Expert Comment

by:CyanBlue
Comment Utility
If you changed all of them to GET and it is still not working, this must be a problem with your host...  Check with the host or whoever set up the server...  I know that script works cuz that's mine...

CyanBlue
0
 

Author Comment

by:xanabobana
Comment Utility
I finally got the email 12 hours later! it's working w/ $_GET thanks!
0
 
LVL 37

Expert Comment

by:CyanBlue
Comment Utility
Glad to hear that you've got it working...  I don't understand why it takes 12 hours to get emails...  You definitely want to check with sys admin for that...

NOW!!!  You need to change all the GET to POST both in FLA and PHP...  The reason why I was asking it to use GET is to see the trace output inside the Flash to make sure everything is going as it should be...  If you don't change it to POST, it is so easy to anybody to call up your PHP script and send spam emails, so it's highly recommended to change it back to POST...

CyanBlue
0
 

Author Comment

by:xanabobana
Comment Utility
will do that- thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now