-3

my php contact form works and sends me an email when people fill it out. what is happinging though is when they click submit, the screen goes white and nothing happens. I want the page to refresh or redirect to another page. How Can I get it to refresh?

here is my PHP:

<?php 

// define variables and set to empty values
$name_error = $email_error = "";
$name = $email = $message = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
 $name_error = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $name_error = "Only letters and white space allowed"; 
    }
  }

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

  if (empty($_POST["message"])) {
    $message = "";
  } else {
    $message = test_input($_POST["message"]);
  }

  if ($name_error == '' and $email_error == '' ){
      $message_body = '';
     unset($_POST['submit']);
     foreach ($_POST as $key => $value){
        $message_body .=  "$key: $value\n";
   }

      $to = 'whitandr@oregonstate.edu';
      $subject = 'Contact Form Submit';
     if (mail($to, $subject, $message_body)){
      $success = "Message sent, thank you!";
      $name = $email = $message = '';
  }
 }

}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
 $data = htmlspecialchars($data);
  return $data;

}

  • Provide the html form and the file name for each code part you are presenting. Shouldn't you have a `require 'functions.php';` (or similar) in your php code? – PajuranCodes Apr 02 '18 at 01:03
  • you can use = " ";?> then it will reload, meta should be place in tag but it will work same as header('Location: yourpage.php'); after the codewas executed the echo'd meta will be lost when it refresh! – bdalina Apr 02 '18 at 01:16

1 Answers1

0

You can add this to the end of your script to redirect the user header('Location: success.php');

AJK
  • 391
  • 9
  • 27