Reputation:
I am not sure what is the best way to achieve this. I am trying to make the input button on my PHP form to clear the form if users want to send another text. At the moment I added an event onClick="history.go(-1);return (true);" but the form still appears filled. Is there anyway to achieve this by adding an event to the button.
echo "<div id='success_page_apps'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you, your message has been submitted to us.</p>";
echo "</div>";
echo "<input type="button" value="Send Another" onClick="history.go(-1); return (true); ">";
Or else should I change something on the rest of the code:
$error = '';
$email = '';
$comments = '';
if(isset($_POST['contactus'])) {
$email = $_POST['email'];
$comments = $_POST['comments'];
$app = $_SERVER["REQUEST_URI"];;
if(trim($comments) == '') {
$error = '<div class="error_message">Attention! Please enter your message.</div>';
} else if(trim($email) == '') {
$error = '<div class="error_message">Attention! Please enter a valid email address.</div>';
} else if(!isEmail($email)) {
$error = '<div class="error_message">Attention! You have enter an invalid e-mail address, try again.</div>';
}
if($error == '') {
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
$address = "[email protected]";
$e_subject = 'You\'ve been contacted from an app web page ' . $name . '.';
$e_body = "You have been contacted using the app comments box on the above app web page, their additional message is as follows.\r\n\n";
$e_content = "\"$comments\"\r\n\n";
$e_reply = "$name $email";
$msg = $e_body . $e_content . $e_reply;
mail($address, $e_subject, $msg, $app, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
echo "<div id='success_page_apps'>";
echo "<h1>Email Sent Successfully.</h1>";
echo "<p>Thank you, your message has been submitted to us.</p>";
echo "</div>";
echo '<input type="button" value="Send Another" onClick="history.go(-1); return (true); ">';
}
}
if(!isset($_POST['contactus']) || $error != '') // Do not edit.
{
?>
<?php echo $error; ?>
<fieldset id="contact_apps">
<br />
<form method="post" action="#ContactForm">
<label for="email" accesskey="E"><span class="required"></span> Email</label>
<input name="email" type="text" id="email" size="33" value="<?php echo$email;?>" />
<textarea name="comments" cols="50" rows="15" id="comments"><?php echo$comments;?></textarea>
<input name="contactus" type="submit" class="send" id="contactus" value="Submit">
</form>
</fieldset>
<?php }
UPDATE
Great thanks to Edwin Alex and Bojangles for pointing me to jQuery to resolve the problem.
I have sort out the problem by adding the following at the bottom of my php page right after the tag.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#email").val('');
$("#comments").val('');
});
Upvotes: 0
Views: 992
Reputation: 688
You should always redirect your user to a new page using header("Location: ..."); Otherwise, the post values stay in memory and a page refresh will resend them.
Upvotes: 0
Reputation: 5108
You can use :input selector to achieve this.
$(":input").val('');
This single statement will clear all the values in the form fields.
Upvotes: 1