Zyggy Posted September 11, 2014 Share Posted September 11, 2014 Hi folks! I have been using relatively clean and simple PHP contact forms for quite some time now. They fit my needs and are easy to work with. However, after submitting the message, it takes me to a gray screen with the nothing but text I programmed it to echo. It isn't an error or bug, but it's an ugly redirect and does not look professional. I would like to learn how to work with this in one of two ways: either submit the message without leaving the contact page, or add a redirect of some sort. This is the contact.php file I use. If anyone has input on how to do this, it'd be greatly appreciated!! <?phpfunction spamcheck($field) { //filter_var() sanitizes the e-mail //address using FILTER_SANITIZE_EMAIL $field=filter_var($field, FILTER_SANITIZE_EMAIL); //filter_var() validates the e-mail //address using FILTER_VALIDATE_EMAIL if(filter_var($field, FILTER_VALIDATE_EMAIL)) { return TRUE; } else { return FALSE; } }if (isset($_REQUEST['email'])) {//if "email" is filled out, proceed //check if the email address is invalid $mailcheck = spamcheck($_REQUEST['email']); if ($mailcheck==FALSE) { echo "Invalid input. Please enter a valid email address."; } else {//send email $name = $_POST['name'] ; $email = $_POST['email'] ; $message = $_POST['message'] ; $totalmessage = " Name: $name n Details: $message n"; mail("email@email.com", "Email: $email", $totalmessage, "From: $email" ); echo "Thank you for contacting me. I'll get back with you ASAP."; } }else { //if "email" is not filled out, display the form?> <form method="post" action="contact.php"> <div class="row half"> <div class="6u"><input type="text" name="name" placeholder="Name" /></div> <div class="6u"><input type="email" name="email" placeholder="Email" /></div> </div> <div class="row half"> <div class="12u"><textarea name="message" placeholder="Message" rows="6"></textarea></div> </div> <div class="row"> <div class="12u"> <ul class="actions"> <li><input type="submit" value="Send Message" name="submit" /></li> </ul> </div> </div> </form><?php}?> Link to comment Share on other sites More sharing options...
niche Posted September 11, 2014 Share Posted September 11, 2014 Have you considered ajax? http://www.w3schools.com/php/php_ajax_intro.asp Link to comment Share on other sites More sharing options...
Zyggy Posted September 11, 2014 Author Share Posted September 11, 2014 That looks like exactly what I need. Thank you very much! Link to comment Share on other sites More sharing options...
guruparthi Posted September 12, 2014 Share Posted September 12, 2014 (edited) You can also done this in ajax with jquery. If you want to send message without page refresh, the better idea is ajax. On form submit, you post values to another page. for sample $.ajax({url:'next.php',data:$('#form').serialize(),type:'POST',success:function(a) { $('.res').html('Your msg send successfull'); }}); Otherwise you redirect next page if the mail is send successfully. For exmple, $mail=mail($to,$subject,$msg,$header); if($mail) { header('location:next.php'); } Edited September 12, 2014 by Ingolme Advertising Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now