Solved

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

Posted on 2011-02-27
19
713 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
ID: 34992615
$ 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
ID: 34998531
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
ID: 35031416
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:xanabobana
ID: 35060759
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
ID: 35060800
changed the php file to match the tutorial's too...
0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 35060836
Can you post the updated codes???

CyanBlue
0
 

Author Comment

by:xanabobana
ID: 35061357

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
ID: 35061587
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
 

Author Comment

by:xanabobana
ID: 35063521
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
ID: 35064071
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
ID: 35064394

     
<?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
ID: 35064858
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
ID: 35064918
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
ID: 35065117
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
ID: 35084883
I finally got the email 12 hours later! it's working w/ $_GET thanks!
0
 
LVL 37

Expert Comment

by:CyanBlue
ID: 35085063
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
ID: 35085115
will do that- thanks!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In addition to being a great web-based presentation tool, Prezi also makes it easy to save your presentation as a PDF to share with others as well. Learn how in this tutorial. Select the share icon from the top menu in your Prezi editor: Select "D…

773 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