1

I have an html input form as well as a php email script that takes these values on the same page.

The problem is that before I submit any data into the forms I get a blank email because my php script is not waiting for the user input.

I don'y wan't to use another page for my email script because I don't want to pass variables through GET and I don't know how to implement sessions yet.

Thanks and here is my code

<div id = "center">
<form action="post.php" name="emailform" method="post">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" value="Send Email">
</form>
</div>

<?php
if (!isset($_POST['submit'])) {
    echo 'you have hit the submit button';

    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];

    $email_from = 'trustyclient@yoursite.com';
    $email_subject = "Message from client";
    $email_body = "Message from: $visitor_email \n \n Message:$message";


    $to = "myemail@myemail.com";
    $headers = "from:adam\r\n";
    mail($to,$email_subject,$email_body,$headers);
} else {
    echo 'You have not hit the submit button yet';  
}       
?>
nobody
  • 19,814
  • 17
  • 56
  • 77
user970638
  • 129
  • 1
  • 2
  • 9
  • php scripts do not 'wait' for input... they run. Run another script. Which does not imply any session by the way... the other script can catch your form as well as the first. here some reading that might unconfuse you about server and client side: http://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Félix Adriyel Gagnon-Grenier Oct 20 '14 at 19:54

2 Answers2

4

First, give your submit button a name, like 'submit' (because you've already referenced that name in the PHP). Example:

<input type="submit" name="submit" value="Send Email">

Now you can actually use $_POST['submit'] in your code.

Then another tweak:
When you state if (!isset($_POST['submit'])) {, the following code runs if the submit button has not been pressed, because of the !. To fix, just remove the !, making it:

if (isset($_POST['submit'])) {

! tells the if statement to evaluate to true if the following expression, here isset($_POST['submit']), evaluates to false. Therefore ! means "if the opposite".

NB: Also, the concept that the PHP runs when the submit button is pressed is slightly off. The submit button triggers that page to load a different page (or the same page). The PHP code runs only once when the page loads.

ajndl
  • 395
  • 1
  • 7
  • Thanks for the help. Even with your example the script defaults to my else statement because the user cannot press the button fast enough..and then the script is finished. I understand what you said with your edit but I'm not sure how else to accomplish this – user970638 Oct 20 '14 at 03:26
  • @user970638 I think you are assuming that the script runs only once. Your current PHP runs every time you load post.php, even when you load the page the first time. When you type `post.php` into the browser, the PHP is run, and because you haven't pressed submit, you are getting that message. When you click on the button, it ***reloads*** the page, and because the `$_POST['submit']` is set, the mail script will run. Just remove the whole else statement and see if clicking the button still does what you want. – ajndl Oct 20 '14 at 03:32
  • Removing the else statement still does not send the email. Its as if the page is refreshing and resetting the submit button. I know the script works because without the if statements I get an email on every page load – user970638 Oct 20 '14 at 03:55
  • @user970638 Try giving the submit button a name and using that instead of `'submit'` in the $_POST array. Example: `` and `if (!isset($_POST['send'])) {` – ajndl Oct 20 '14 at 15:00
  • Nice! giving a value to the name attribute and using that name worked. If you want to reflect that in your answer I will accept it. – user970638 Oct 20 '14 at 18:28
0

Try this.

<div id = "center">
<form action="post.php" name="emailform" method="post">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" value="Send Email">
</form>

</div>


<?php


if (isset($_POST['submit'])) {


echo 'you have hit the submit button';

    if (empty(trim($_POST['name'])) || empty(trim($_POST['email'])) || empty(trim($_POST['message']))) {
      echo 'Some fields are empty.';
    } else {

    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];

    $email_from = 'trustyclient@yoursite.com';
    $email_subject = "Message from client";
    $email_body = "Message from: $visitor_email \n \n Message:$message";


    $to = "myemail@myemail.com";
    $headers = "from:adam\r\n";
    mail($to,$email_subject,$email_body,$headers);
    }
 } else {
 
 
 echo 'You have not hit the submit button yet';
 
 }
 
 ?>
Efog
  • 1,155
  • 1
  • 15
  • 33