simcoweb Posted July 3, 2007 Share Posted July 3, 2007 Ok, I have a form that when submitted submits all the data to MySQL and then sends out an email to each party if query is successful. Pretty straightforward stuff. I have an 'include' file that holds my database parameters AND my functions. So, I have this function in the included file: // send email results and confirmation function function send_mails() { // start mail process $mailContent="--------CONTACT--------\n" ."Name: ".$name."\n" ."Date: ".$date."\n" ."E-mail: ".$email."\n\n--------PHONE--------\n" ."Phone: ".$phone."\n" ."Address: ".$address."\n" ."City: ".$city."\n" ."State: ".$state."\n" ."Zip: ".$zip."\n\n---------Other Details---------\n" ."When are you moving: ".$moving."\n" ."Where are you moving: ".$where."\n" ."Your price range: ".$price."\n" ."Number of bedrooms: ".$bedrooms."\n" ."Square footage: ".$sqft."\n" ."Comments: ".$comments."\n"; //---------------------------------- $toAddress="[email protected]"; /* change this! */ $subject="Seattle Viet Homes Buyer Inquiry"; /* change this! */ $recipientSubject="Seattle Viet Homes Buyer Inquiry"; /* change this! */ $receiptMessage = "Thank you ".$name." for inquiring at SeattleVietHomes.com's website!\n\n\nHere is the contact information you submitted to us:\n\n" ."--------CONTACT--------\n" ."Name: ".$name."\n" ."E-mail: ".$email."\n" ."Phone: ".$phone."\n"; //---------------------------------- mail($email, $subject, $receiptMessage,"From:$toAddress"); //---------------------------------- mail($toAddress,$recipientSubject,$mailContent,"From:$email"); } Then, in my form page I have this code to run the function IF the query is successful and no errors. Everything else works but the emails don't get sent. here's the function call: $results = mysql_query($sql) or die(mysql_error()); if (mysql_affected_rows($dbc) == 1){ // Send the E-Mail send_mails(); // redirect to confirmation page header("Location: confirmation.htm"); } else { header("Location: error.htm"); } If I take the send_mail(); code and put it into the form page it works fine. I'm not understanding why it won't work if I 'call' the function from another file. Ideas? Link to comment https://forums.phpfreaks.com/topic/58318-solved-cant-understand-why-this-function-wont-function/ Share on other sites More sharing options...
DeathStar Posted July 3, 2007 Share Posted July 3, 2007 eh... function send_mails() { // start mail process $mailContent = "--------CONTACT--------\n"; $mailContent .= "Name: ".$name."\n"; $mailContent .= "Date: ".$date."\n"; $mailContent .= "E-mail: ".$email."\n\n--------PHONE--------\n"; $mailContent .= "Phone: ".$phone."\n"; $mailContent .= "Address: ".$address."\n"; $mailContent .= "City: ".$city."\n"; $mailContent .= "State: ".$state."\n"; $mailContent .= "Zip: ".$zip."\n\n---------Other Details---------\n"; $mailContent .= "When are you moving: ".$moving."\n"; $mailContent .= "Where are you moving: ".$where."\n"; $mailContent .= "Your price range: ".$price."\n"; $mailContent .= "Number of bedrooms: ".$bedrooms."\n"; $mailContent .= "Square footage: ".$sqft."\n"; $mailContent .= "Comments: ".$comments."\n"; /*--------------------------------------------------------------*/ $toAddress = "[email protected]"; // change this! $subject = "Seattle Viet Homes Buyer Inquiry"; // change this! $recipientSubject = "Seattle Viet Homes Buyer Inquiry"; // change this! $receiptMessage = "Thank you ".$name." for inquiring at SeattleVietHomes.com's website!\n\n\n"; $receiptMessage .= "Here is the contact information you submitted to us:\n\n"; $receiptMessage .= "--------CONTACT--------\n"; $receiptMessage .= "Name: ".$name."\n"; $receiptMessage .= "E-mail: ".$email."\n"; $receiptMessage .= "Phone: ".$phone."\n"; /*--------------------------------------------------------------*/ mail($email, $subject, $receiptMessage,"From:$toAddress"); /*--------------------------------------------------------------*/ mail($toAddress,$recipientSubject,$mailContent,"From:$email"); } Link to comment https://forums.phpfreaks.com/topic/58318-solved-cant-understand-why-this-function-wont-function/#findComment-289164 Share on other sites More sharing options...
simcoweb Posted July 3, 2007 Author Share Posted July 3, 2007 Ok, so what you're saying is that IF I want to run that code as a 'function' that I have to list each line as a variable? Link to comment https://forums.phpfreaks.com/topic/58318-solved-cant-understand-why-this-function-wont-function/#findComment-289175 Share on other sites More sharing options...
Barand Posted July 3, 2007 Share Posted July 3, 2007 You have a lot of variables inside that function that aren't defined. http://us2.php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/58318-solved-cant-understand-why-this-function-wont-function/#findComment-289204 Share on other sites More sharing options...
simcoweb Posted July 3, 2007 Author Share Posted July 3, 2007 Ahhh, ok. This goes back to one of my earlier lessons...about 'global' declarations within a function. So, if i'm on track with this, I need to declare those variables as 'global' in order to have them populate plus execute. Correct? Link to comment https://forums.phpfreaks.com/topic/58318-solved-cant-understand-why-this-function-wont-function/#findComment-289210 Share on other sites More sharing options...
Barand Posted July 3, 2007 Share Posted July 3, 2007 Or, better, pass them to the function as arguments. This could be a single array arg if that's easier. Link to comment https://forums.phpfreaks.com/topic/58318-solved-cant-understand-why-this-function-wont-function/#findComment-289214 Share on other sites More sharing options...
simcoweb Posted July 4, 2007 Author Share Posted July 4, 2007 Figured it out. Thanks for pointing that out, Barand. I forgot lesson 101 on variables and functions Link to comment https://forums.phpfreaks.com/topic/58318-solved-cant-understand-why-this-function-wont-function/#findComment-289284 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.