Reputation: 3
I am trying to create a simple forum using php to email the contents. Once the user clicks submit, I would like to have the div display a thank you message.
Currently going about this by trying to have the php write the html code using an if else statement. It seems the if statement is working, but the else statement is showing up as text.
Here is my code:
<?php
if(isset($_POST['submit'])) {
echo "<form method="post" action="">
<div class="field half first">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div class="field half">
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message" rows="5"></textarea>
</div>
<input class="button submit" type="submit" name="submit" value="Submit">
</form>";
} else {
$email_to = "[email protected]";
$email_subject = "Field 25 Contact Form";
echo "Thanks! We'll get back to you soon.";
}
?>
You can see the problem in the bottom of the page here if you'd like: http://field25.com/
I'm also open to other ways of achieving this, thanks for you help.
Upvotes: 0
Views: 3130
Reputation: 4155
Surprised that's rendering anything at all! There are a couple of problems. First, you have nested double quotes. Second, you're checking for the existence of the submit
and if it exists, rendering the form. If it has to exist first, it'll never be rendered.
You can avoid the first problem entirely by simply dipping out of PHP. The second problem can be fixed by checking if submit
does not exist via the !
operator (or flipping the if/else output).
<?php
// if submit is not set, we want to render the form.
if( !isset($_POST['submit']) ) { ?>
<!-- leave PHPlandia -->
<form method="post" action="">
<div class="field half first">
<label for="name">Name</label>
<input type="text" name="name" id="name" />
</div>
<div class="field half">
<label for="email">Email</label>
<input type="text" name="email" id="email" />
</div>
<div class="field">
<label for="message">Message</label>
<textarea name="message" id="message" rows="5"></textarea>
</div>
<input class="button submit" type="submit" name="submit" value="Submit">
</form>
<?php
} else {
$email_to = "[email protected]";
$email_subject = "Field 25 Contact Form";
// ... rest of your email sending code here ...
echo "Thanks! We'll get back to you soon.";
}
?>
Upvotes: 1