user970638
user970638

Reputation: 129

PHP: Wait for User Input Before Executing Script

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';  
}       
?>

Upvotes: 1

Views: 12736

Answers (2)

ajndl
ajndl

Reputation: 394

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.

Upvotes: 4

Efog
Efog

Reputation: 1179

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';
	
	}
	
	?>

Upvotes: 0

Related Questions