0

I have a form built that works perfectly fine. However, when a message is successfully submitted, the user gets redirected to a new page with the 'success' message I have set up. Instead, I want the success message to be displayed in a div which is placed next to the form, and the form to reset in case the user would like to send another message. Likewise, I am also hoping to have my 'error' message show up in the same div upon failure. Was hoping someone can help with my if/else statement to make this possible.

Here's my HTML:

<div id="contact-area">

<form id="theform" name="theform" method="post" action="feedback.php">
<input type="hidden" name='sendflag' value="send">

<p>
<label for="Name">Name:</label>
<input type="text" name="name" id="name" value="" />
</p>

<p>
<label for="Email">Email:</label>
<input type="text" name="email" id="email" value="" />
</p>

<p>
<label for="Message">Message:</label><br />
<textarea name="message" rows="20" cols="20" id="message"></textarea>
</p>

<p>
<input type="submit" name="submit" value="Submit" class="submit-button" />
</p>

</form>     
</div>

<div class="message">
<p class="submitMessage"></p>
</div>

Here's my PHP:

<?php

$mail_to_send_to = "[email protected]";
$your_feedbackmail = "[email protected]";

$sendflag = $_REQUEST['sendflag'];
if ( $sendflag == "send" )
{
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $name" . "\r\n" . "Reply-To: $email" . "\r\n" ;
$a = mail( $mail_to_send_to, "Feedback form", $message, $headers );
if ($a)
{
print("Message was sent, you can send another one");
} else {
print("Message wasn't sent, please check that you have changed emails in the bottom");
}
}

?>
2
  • 3
    I suggest you look into ajax forms and javascript. There is no specific error here, just a "how do I do/show me teh codez" Dec 20, 2012 at 6:07
  • I agree with Alastair. It is not difficult what you want, a google for ajax jquery form will give you hundreds of tutorials. Also a search here will provide enough examples to get started and come back if you get stuck on actual bits
    – mplungjan
    Dec 20, 2012 at 6:09

2 Answers 2

0

If I understand your question correctly, you're looking to never leave a page but rather have a div appear or disappear based off of a successful form submission. If so, it looks like you're going to have to use AJAX. Luckily, jQuery has this built right in! I'd suggest something like the following:

$.post("url.php", { option1: value1, option2: value2 }, function(data) {
    if(data != '')
        $('#theDiv').html("Success!");
});

For more information, read up on the documentation here.

0

Read and follow examples here: jQuery AJAX

You'll basically do something like this.

$.ajax({
  url: /url/to/your/php,
  data: dataObjectPosting
  success: function(data){
    //the data object will have your PHP response;
    $('#divid').text('success message');
  },
  error: function(){
    alert('failure');
  }
});

Remember, success simply means HTTP 200, not necessarily that your PHP code ran successfully as you would see it.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.