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 = '[email protected]';
    $email_subject = "Message from client";
    $email_body = "Message from: $visitor_email \n \n Message:$message";


    $to = "[email protected]";
    $headers = "from:adam\r\n";
    mail($to,$email_subject,$email_body,$headers);
} else {
    echo 'You have not hit the submit button yet';  
}       
?>
1
  • 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: stackoverflow.com/questions/13840429/… Commented Oct 20, 2014 at 19:54

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
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 Try giving the submit button a name and using that instead of 'submit' in the $_POST array. Example: <input type="submit" name="send" value="Send Email"> and if (!isset($_POST['send'])) {
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.
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 = '[email protected]';
    $email_subject = "Message from client";
    $email_body = "Message from: $visitor_email \n \n Message:$message";


    $to = "[email protected]";
    $headers = "from:adam\r\n";
    mail($to,$email_subject,$email_body,$headers);
    }
	} else {
	
	
	echo 'You have not hit the submit button yet';
	
	}
	
	?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.