Jump to content

Redirecting after submission of contact form


Recommended Posts

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 post
Share on other sites

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 by Ingolme
Advertising
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...