agregoire Posted March 22, 2009 Share Posted March 22, 2009 I am attempting to display all php variables from a form onto a different page which works fine. On this page, I want the user to review the information and then press a button to send the information via email. I am trying to use a php if statement that checks to see if a variable is set, if it is set to 1 then the mail will send using the php mail function. The problem is that when the page loads the mail is set even though the php variable is set to 0 and not 1. Here is my code: $button = 0; //button used as link to end page and used to set $button = 1 <a href="employee_hours_finish.html"><button name="Send Hours" type="button" onclick="<?php $button = 1; ?>" >Send Hours</button></a> <?php if($button == 1){ mail($to,$subject,$message,$headers); } else{} ?> Once again, mail($to,$subject,$message,$headers); is called regardless of what $button is set to. Quote Link to comment https://forums.phpfreaks.com/topic/150615-php-if-statement-does-not-work/ Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 php does not respond to onclick events. They execute client-side while php executes server side. Quote Link to comment https://forums.phpfreaks.com/topic/150615-php-if-statement-does-not-work/#findComment-791141 Share on other sites More sharing options...
Maq Posted March 22, 2009 Share Posted March 22, 2009 You should use and isset function instead. Quote Link to comment https://forums.phpfreaks.com/topic/150615-php-if-statement-does-not-work/#findComment-791142 Share on other sites More sharing options...
agregoire Posted March 22, 2009 Author Share Posted March 22, 2009 Is there anyway to use a button to invoke a php mail function? I'm new to php and getting frustratred that this doesn't seem to work anyway i do it. Quote Link to comment https://forums.phpfreaks.com/topic/150615-php-if-statement-does-not-work/#findComment-791151 Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 <form method="get"><input type="submit" name="submit" type="button" value="Send Hours"></form> <?php if (isset($_GET['submit'])) { // execute function. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150615-php-if-statement-does-not-work/#findComment-791156 Share on other sites More sharing options...
Maq Posted March 22, 2009 Share Posted March 22, 2009 if(isset($_POST['submit'])){ $to = $_POST['to']; $subject = $_POST['subject']; $message = $_POST['message']; $headers = "headers"; if(mail($to,$subject,$message,$headers)) { echo "mail sent"; } else { echo "error occured"; } } ?> </pre> <form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="POST"> To: Subject: Message: < Quote Link to comment https://forums.phpfreaks.com/topic/150615-php-if-statement-does-not-work/#findComment-791165 Share on other sites More sharing options...
agregoire Posted March 23, 2009 Author Share Posted March 23, 2009 This solutions work but not with the logic i'm trying to invoke for my client. Here is what needs to happen. 1. user enters information on form, form is submitted to itself for error checking, if no errors the information is displayed on new page for customer to review. 2. after the customer reviews, they press submit button, this sends a php mail using all the variables from the form on the previous page. Problems that I need help with: 1. I try to delay the <?php mail($to,$subject,$body) ?> function by using a button and an if statement to invoke, but the mail is sent regardless of the conditions of the if statment. 2. I try to remedy this by using a new form, and using if(isset($_POST['submit'])){ and the following form code, <form method="get" name="email" action="<?php echo $PHP_SELF;?>"> <input class="submit" type="submit" name="submit" type="button" value="Send Hours"> </form> <?php if (isset($_GET['submit'])) { mail($to,$subject,$message,$headers); } ?> This sends the mail but it does not contain the variable information, it sends a blank email. How can I get the variable information to show up in the email, it seems that it is lost when i press submit on the second page. Quote Link to comment https://forums.phpfreaks.com/topic/150615-php-if-statement-does-not-work/#findComment-791881 Share on other sites More sharing options...
Maq Posted March 23, 2009 Share Posted March 23, 2009 This sends the mail but it does not contain the variable information, it sends a blank email. Of course it sends a blank email, you're using variables that don't have any values! You should use the POST method when dealing with forms... </pre> <form method="get"> should be: [code] And when you get the submission you have to retrieve these values with the method you stated in your form, POST. Where do these values come from? [code]$to,$subject,$message,$headers< Quote Link to comment https://forums.phpfreaks.com/topic/150615-php-if-statement-does-not-work/#findComment-791894 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.