trouble passing an event listener var to php

  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

Hi everybody,
I have a combobox that is read by an event listener. If I trace the var from the listener I get the right information. The thing I am stumped about is how to then pass this variable to my php mailer. Any help would be appriciated. here is my AS2 code:

Code: [ Select ]
myComboBoxListener = new Object();
myComboBoxListener.change = function(eventObj) {
   var eventSource = eventObj.target;
   var theSelectedItem = eventSource.selectedItem;
   var theSelectedItemLabel = theSelectedItem.data;
   trace("You selected "+theSelectedItemLabel+".");
};
myComboBox.addEventListener("change", myComboBoxListener);
  1. myComboBoxListener = new Object();
  2. myComboBoxListener.change = function(eventObj) {
  3.    var eventSource = eventObj.target;
  4.    var theSelectedItem = eventSource.selectedItem;
  5.    var theSelectedItemLabel = theSelectedItem.data;
  6.    trace("You selected "+theSelectedItemLabel+".");
  7. };
  8. myComboBox.addEventListener("change", myComboBoxListener);
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

Well you have two issues. First your building a variable that only exists within the .change function. Due to scope issues your going to have a hard time accessing it outside the function.

To solve this you need to create a variable outside the function and then set its value when the on change fires. So:
ACTIONSCRIPT Code: [ Select ]
// create holder varialbes
var selItem = "";
var selLabel = "";
 
myComboBoxListener = new Object();
  myComboBoxListener.change = function(eventObj) {
  var eventSource = eventObj.target;
  var theSelectedItem = eventSource.selectedItem;
  var theSelectedItemLabel = theSelectedItem.data;
  // attach your vars to the holder vars
  selItem = theSelectedItem;
  selLabel = theSelectedLabel;
 
  // trace the variables here to show that they've changed
  trace("item = "+selItem+" and label= "+selLabel);
};
myComboBox.addEventListener("change", myComboBoxListener);
 
// If you trace the vars here they'll be blank because the event hasn't fired yet.
trace("item = "+selItem+" and label= "+selLabel);
 
 
  1. // create holder varialbes
  2. var selItem = "";
  3. var selLabel = "";
  4.  
  5. myComboBoxListener = new Object();
  6.   myComboBoxListener.change = function(eventObj) {
  7.   var eventSource = eventObj.target;
  8.   var theSelectedItem = eventSource.selectedItem;
  9.   var theSelectedItemLabel = theSelectedItem.data;
  10.   // attach your vars to the holder vars
  11.   selItem = theSelectedItem;
  12.   selLabel = theSelectedLabel;
  13.  
  14.   // trace the variables here to show that they've changed
  15.   trace("item = "+selItem+" and label= "+selLabel);
  16. };
  17. myComboBox.addEventListener("change", myComboBoxListener);
  18.  
  19. // If you trace the vars here they'll be blank because the event hasn't fired yet.
  20. trace("item = "+selItem+" and label= "+selLabel);
  21.  
  22.  


Now depending on what you need to do with the variables in php you could make the call right inside the on change function but its probably easier to have a separate function to handle all that which you call inside the listener.

ACTIONSCRIPT Code: [ Select ]
// create holder varialbes
var selItem = "";
var selLabel = "";
 
myComboBoxListener = new Object();
  myComboBoxListener.change = function(eventObj) {
  var eventSource = eventObj.target;
  var theSelectedItem = eventSource.selectedItem;
  var theSelectedItemLabel = theSelectedItem.data;
  // attach your vars to the holder vars
  selItem = theSelectedItem;
  selLabel = theSelectedLabel;
 
  // function to run
  cbchanged();
 
};
myComboBox.addEventListener("change", myComboBoxListener);
 
// function to run on change
function cbchanged(){
  trace("cb changed. new item = "+selItem+" and new label= "+selLabel);
}
 
  1. // create holder varialbes
  2. var selItem = "";
  3. var selLabel = "";
  4.  
  5. myComboBoxListener = new Object();
  6.   myComboBoxListener.change = function(eventObj) {
  7.   var eventSource = eventObj.target;
  8.   var theSelectedItem = eventSource.selectedItem;
  9.   var theSelectedItemLabel = theSelectedItem.data;
  10.   // attach your vars to the holder vars
  11.   selItem = theSelectedItem;
  12.   selLabel = theSelectedLabel;
  13.  
  14.   // function to run
  15.   cbchanged();
  16.  
  17. };
  18. myComboBox.addEventListener("change", myComboBoxListener);
  19.  
  20. // function to run on change
  21. function cbchanged(){
  22.   trace("cb changed. new item = "+selItem+" and new label= "+selLabel);
  23. }
  24.  


Obviously there are a lot of ways to simplify that code but its good to know whats going on.

Second there are a couple ways to pass that data out to php/javascript etc. Take a look at both sendAndLoadVars and externalInterface to find the best one for your setup.
  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

Thanks so much for your input. After reading your post I was able to pass the comboBox variable out of the .choice function into a global variable.

I can now see the variable outside the function when I trace it.
Everything seems to be working fine but when I try to take the global variable and send it to the php mailer along with the other ones it still doesn't want to go .... hmm stumped!

here is my code any feedback would be appriciated.

Submit button code:
Code: [ Select ]
            loadVariables("wasserMail.php", "", "POST");
            
            showerror("Sending data...");

            varSender = new LoadVars();
            varSender.cname = cname;
            varSender.email = email;
            varSender.company = company;
            varSender.msg = msg;
            
            varSender.selLabel = selLabel;
            trace (selLabel);
            trace (_global.selLabel);

            varSender.sendAndLoad(url, varSender, "POST");
  1.             loadVariables("wasserMail.php", "", "POST");
  2.             
  3.             showerror("Sending data...");
  4.             varSender = new LoadVars();
  5.             varSender.cname = cname;
  6.             varSender.email = email;
  7.             varSender.company = company;
  8.             varSender.msg = msg;
  9.             
  10.             varSender.selLabel = selLabel;
  11.             trace (selLabel);
  12.             trace (_global.selLabel);
  13.             varSender.sendAndLoad(url, varSender, "POST");



And here is my php code
Code: [ Select ]
$to=(isset($_POST['selLabel'])) ? $_POST['selLabel'] : false;
$cname=(isset($_POST['cname'])) ? $_POST['cname'] : false;
$email=(isset($_POST['email'])) ? $_POST['email'] : false;
$company=(isset($_POST['company'])) ? $_POST['company'] : false;
$msg=(isset($_POST['msg'])) ? $_POST['msg'] : false;
$result=(isset($_POST['result'])) ? $_POST['result'] : false;



$body = "
Wasser Website - E-mail Contact: \n
Name: $cname \n
Email: $email \n
Company: $company \n
Message: $msg \n
To: $to \n

";


$subject = "Wasser Website - E-mail Contact";
mail($to, $subject, $body);
  1. $to=(isset($_POST['selLabel'])) ? $_POST['selLabel'] : false;
  2. $cname=(isset($_POST['cname'])) ? $_POST['cname'] : false;
  3. $email=(isset($_POST['email'])) ? $_POST['email'] : false;
  4. $company=(isset($_POST['company'])) ? $_POST['company'] : false;
  5. $msg=(isset($_POST['msg'])) ? $_POST['msg'] : false;
  6. $result=(isset($_POST['result'])) ? $_POST['result'] : false;
  7. $body = "
  8. Wasser Website - E-mail Contact: \n
  9. Name: $cname \n
  10. Email: $email \n
  11. Company: $company \n
  12. Message: $msg \n
  13. To: $to \n
  14. ";
  15. $subject = "Wasser Website - E-mail Contact";
  16. mail($to, $subject, $body);
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

Do you get any of the varSender data in the PHP? Also what does the trace produce when you hit submit?
  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

I get all the varSender data but the "varSender.selLabel = selLabel;"
And when I hit submit the trace produces the correct data item chosen from the comboBox ....
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

do both these produce the same thing?
# trace (selLabel);
# trace (_global.selLabel);

Also try tracing:
varSender.selLabel
  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

All 3 traces return the chosen item from the comboBox...

If I can trace the varSender.selLabel then I'm assuming that there is maybe something wrong with my php .... weird
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

That was my thought too. Unless there's the off chance that its passing (and tracing) an object reference instead of a string that PHP doesn't understand.

try this:
ACTIONSCRIPT Code: [ Select ]
var selLabelString = selLabel.toString();
varSender.selLabel = selLabelString;
 
  1. var selLabelString = selLabel.toString();
  2. varSender.selLabel = selLabelString;
  3.  
  • beaner
  • Novice
  • Novice
  • beaner
  • Posts: 15

Post 3+ Months Ago

hmmm I put the code you write in my submit button but it still isn't passing anything .. i'm so out of ideas ...

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 13 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.