php - Dynamically replace form with message upon submission


I've written a basic PHP script to generate an email when my contact form is submitted, but as it stands, it redirects the user to an error or thankyou page, which is a little clunky. I'd love to dynamically replace the entire form (or, at least, the submit button) with an error or success message. The problem here is that my PHP knowledge is limited.


<form role="form" method="post" action="contact-form.php">
    <input type="text" class="input-field" name="name" id="name" placeholder="Name" required data-errormessage-value-missing="Hey, I need your name!" />
    <input type="email" class="input-field" name="email" id="email" placeholder="Email" required data-errormessage-value-missing="Don't forget your email address!" />
    <textarea name="message" class="textarea-field" id="message" placeholder="Message" required data-errormessage-value-missing="Please enter your message."></textarea>
    <div class="g-recaptcha" data-sitekey="6LeJpAsTAAAAALzJoRZzq4_fovbjjSG2iaxvVHzy"></div>
    <input type="submit" value="Contact Me" class="btn btn-primary btn-xl" />             



// Load reCAPTCHA library
include_once ("autoload.php");

$name = Trim(stripslashes($_POST['name']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$emailFrom = $email;
$emailTo = "";
$subject = "Contact Request";

// Prepare email body text
$body = "<strong>Name:</strong> $name <br /> <strong>Email:</strong> $email <br /> <strong>Message:</strong> $message";

$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "From: $name <$emailFrom>" . "\r\n";

$secret = 'XXX';
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'],$_SERVER['REMOTE_ADDR']);
if ($resp->isSuccess()){
    $success = mail($emailTo,$subject,$body,$headers);
    header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . '/thankyou.html');
    header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . '/error.html');


Any help would be greatly appreciated!

1 Answer: 

Use the following:

    echo 'Your message goes here.';

    <!-- Your form HTML goes here. -->


EDIT: Sorry, I didn't look closely enough at your html. Try the following. Put all the existing PHP code you have into a contact-form.php file. Put the html form you have in index.php. In the PHP code, remove the header statements and replace them with echo statements - whatever message you want to put there.


More Articles

Revert from Python 2.7 to Python 2.6 in Mac OSX 10.6.8 doesn't work even though I've uninstalled Python 2.7

I have mac OSX 10.6.8. My system came with Python 2.6I recently installed Python 2.7 but experienced many problems with it so decided to revert back to Python 2.6 by executing $ sudo rm -rf /Library/Frameworks/Python.framework/Versions/2.7I also removed Python 2.7 from my Applications folder. Still

scala - Should I use collectionAsScalaIterable({java collection}) or Seq({java collection}).flatten?

We're starting to use Scala Test to test our Java application, and I want to test the contents of a Java Collection. We came up with 2 possibilities:JavaConversions.collectionAsScalaIterable(getJavaCollection()) must contain(allOf(item1, item2).inOrder)orSeq(getJavaCollection()).flatten mustEqual Se

authentication - Sitecore - Prevent access to a page, but still show it in the navigation

In Sitecore I have denied access to a particular page for the anonymous user. This works correctly, but it also means that the page does not appear in the navigation menus and sitemap (both XSLT).What I would like is for the user to be able to see the link, but be redirected to a Register/Login page

Apply function to one element of a list in Python

I'm looking for a concise and functional style way to apply a function to one element of a tuple and return the new tuple, in Python.For example, for the following input:inp = ("hello", "my", "friend")I would like to be able to get the following output:out = ("hello", "MY", "friend")I came up with t

scalatest - Confusing type mismatch in Scala

I have:val words = List("all", "the", "words", "all", "the", "counts", "all", "day")val map = Exercise02.count(words.iterator)val expected = Map("all" -> 3, "the" -> 2, "words" -> 1, "counts" -> 1, "day" -> 1)where Exercise02.count is java.util.Iterator[String] => Map[String, Int]

Multi-user mass image uploads with PHP

I'll soon be building a competition microsite which is based entirely around image uploads: in order to enter, users will need to upload an image.I've previously built a similar website on top of a PHP MVC framework, which worked awesomely up until a couple of hours before the competition was due to

pycharm - pydev debugger: CRITICAL WARNING: This version of python seems to be incorrectly compiled

I can't figure out how to fix this PyCharm warning. I started getting it after I migrated my account to a new Mac. (I also just updated PyCharm.) Another symptom is the PyCharm editor is complaining about references to methods on commands like "". Even though when I run my app, it s

How can I use a Java List with Scala's foreach?

This question already has an answer here: Iterating over Java collections in Scala 9 answers

mapreduce - Hadoop difficultie with composite key

I'm using Hadoop to analyze GSOD data ( chose 5 years to executed my experiments (2005 - 2009).I've configured a little cluster and executed a simple MapReduce program that gets the maximum temperature registered for a year.Now I have to create a new MR progr

php - Magento Ajax Shipping Calculation?

See, goto a general product and on the top left of the product info section theres a field where you enter your local POSTCODE and and it instantly gets the shipping cost without refreshing the page.Is there a good Magento Module that will do that?Or How would I go ab