javascript - Show thank you message after form is submitted after PHP validation -
i have looked @ few solutions on here trying different ways of doing none working me.
here form:
<form action="<?php echo htmlspecialchars($_server['php_self']); ?>" method="post" name="contact" role="form"> <?php //if error has occured echo error in red if(isset($errormsg) && $errormsg) { echo "<p style=\"color: red;\">*",htmlspecialchars($errormsg),"</p>\n\n"; } ?> <label for="name"><b>name:</b></label> <input type="text" name="name" id="name" placeholder="enter full name" value="<?php if(isset($_post['name'])) echo htmlspecialchars($_post['name']); ?>"> <label for="email"><b>email:</b></label> <input type="text" name="email" id="email" placeholder="enter valid email address..." value="<?php if(isset($_post['email'])) echo htmlspecialchars($_post['email']); ?>"> <label><b>subject:</b></label> <input type="text" name="subject" id="subject" placeholder="please enter subject matter" value="<?php if(isset($_post['subject'])) echo htmlspecialchars($_post['subject']); ?>"> <label for="query"><b>query:</b></label> <textarea id="query" placeholder="please write message here..." name="query" value="<?php if(isset($_post['query']))echo htmlspecialchars($_post['query']);?>"> </textarea> <input type="submit" name="submit" id="submit" value="submit" class="style-button"> </form>
i using 1 page website contact form @ bottom of page.
how can show thank message inside form when has been submitted , went through validation - without page going top?
php code:
<?php // if submit clicked assign variables if($_post && isset($_post['submit'], $_post['name'], $_post['email'], $_post['subject'], $_post['query'])) { $name = $_post['name']; $email = $_post['email']; $subject = $_post['subject']; $query = $_post['query']; //making sure page goes contact form errors instead of top of page if(!isset($_post['$errormsg'])) { ?> <script> window.location.hash = '#contact-form'; </script> <?php } // if name not entered display errormsg if (!$name) { $errormsg = "please enter name"; } // if email not entered display errormsg elseif (!$email || !preg_match("/^\s+@\s+$/", $email)) { $errormsg = "please enter valid email address"; } // if subject has not entered display errormsg elseif (!$subject) { $errormsg = "please enter subject"; } // if query not entered display errormsg elseif (!$query) { $errormsg = "please enter query"; } else { //send email , redirect confirmation page $toemail = "email@example.com"; $subject2 = "message recieved ". $name.""; $headers = "mime-version: 1.0\n" ."from: \"".$name."\" <".$email.">\n" ."content-type: text/html; charset-iso-8859-1\n"; $body = "email: ".$email."<br>\n" ."subject: ".$subject."<br>\n" ."email: ".$email."<br>\n" ."query: ".$query."<br>\n" ; mail($toemail, $subject, $body, $headers); if(mail($toemail, $subject, $body, $headers)){ $toemail =".$email"; $subject = "confirmation email"; $body = "your email has been sent"; header("location: index.php"); } }} ?>
somewhere, above html, processing form post. should setting $errormsg in there.
rule of thumb self-submitting web page: logic before view. also, avoid if() statement, initialize $errormsg on every page load.
Comments
Post a Comment