The validation section of my contact.php page is sending the user back to my index.php instead of staying on the contacts.php page so the user can correct the reported error. How can I fix this?

My contact.php code follows:

<?php
if(isset($_POST['submit']))
{

$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$type = $_POST['type'];

//Validate first
if(empty($name)||empty($visitor_email)||empty($type)||empty($message)) 
{
    echo "All fields are mandatory!";
  /*  exit; */
}

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
 /*   exit; */
}

    $subject = $_POST['type'];
    $subject2 = "Copy of email sent to foxclone.com";
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name\r\nEmail: $email\r\nMessage: $message";
    if ($subject =="Website Problem") {
            $to="webmaster@foxclone.com" ;
          }
    else {
            $to="help@foxclone.com";
          }

$email_from = $email;
$email_subject = $type;
$email_body = $formcontent.

$headers = "From: $email \r\n";

//Send the email!
mail($to, $subject, $formcontent, $headers) or die("Error1!");
mail($email, $subject2, $formcontent, $headers) or die("Error2!");

//done. redirect to thank-you page.
header('Location: thank-you.html');

// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
    }
}
}   
?> 
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Foxclone</title>

<!--    <meta content-type="text/html" charset="utf-8"> -->
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Links -->
    <link href="https://fonts.googleapis.com/css?family=Montserrat:500,700&display=swap&subset=latin-ext" rel="stylesheet" />
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:400,400i,600&display=swap&subset=latin-ext" rel="stylesheet" />
    <link href="css/bootstrap.css" rel="stylesheet" />
    <link href="css/fontawesome-all.css" rel="stylesheet" />
    <link href="css/swiper.css" rel="stylesheet" />
    <link href="css/magnific-popup.css" rel="stylesheet" />
    <link href="css/styles.css" rel="stylesheet" /> 

    <!-- Favicon  -->
    <link rel="icon" href="images/favicon.png" />
    <!-- a helper script for validating the contact form-->
    <script language="JavaScript" src="js/gen_validatorv31.js" type="text/javascript"></script>
</head>
    <!-- Navbar -->
    <nav class="navbar navbar-expand-md navbar-dark navbar-custom fixed-top">
    <!-- Mobile Menu Toggle Button -->
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-awesome fas fa-bars"></span>
            <span class="navbar-toggler-awesome fas fa-times"></span>
        </button>
    <!-- end of mobile menu toggle button -->
       <div class="collapse navbar-collapse" id="navbarsExampleDefault">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link page-scroll" href="index.php#header">HOME <span class="sr-only">(current)</span></a>
                </li>

            </ul> 
        </div>
    </nav> <!-- end of navbar -->
    <!-- end of navbar -->
   <!-- Header -->
    <header id="header" class="header">
        <div class="header-content">

        </div> <!-- end of header-content -->
    </header> <!-- end of header -->
    <!-- end of header -->

<div id="contact"  class="stylized">
<br />
    <div "myform"> 

  <br />

         <form id="form1" method="POST" action="/">
  <h1>Need help? Have a suggestion? Why not send us an email.</h1>
         <h3>You'll receive a copy for your records</h3>           
            <label>Name</label>
                <input type="text" name="name" >

            <label>Email</label>
                <input type="text" name="email" >

            <select name="type" id="category" size="1">
                <option value=''>Select A Category</option>
                <option value='Questions'>Questions</option>
                <option value="Report Problem">Report Problem</option>
                <option value='Suggestion'>Suggestion</option>
                <option value='Other'>Other</option>
                <option value="Website Problem"> Website Problem</option>
            </select>

<br /><br /> <br />  

            <label>Message</label>
                <textarea  name="message" rows="7"></textarea><br />

            <button type="submit" value="Send" style="margin-top:15px; margin-left: 500px; text-align: center;">Send Email</button>

        <div class="spacer"></div>
<br /><br /><br /><br /><br /><br />  
        </form>
    </div>   <!-- end of form class -->
</div>
<br /><br /><br /><br />

<!--  End of Contact  -->

 <br /><br /> 
<!-- footer  ================================================== -->
<div id="footer">
    <div class="copyright">
        <div class="container">
            <div class="row">
                <div class="col-lg-12"> 
                    <div class="p-small">Copyright © 2019-2020 Andy Hardwick <br />
          Design by Larry Hale</div>

          <p>
           <a href="https://jigsaw.w3.org/css-validator/check/referer">
              <img style="border:0;width:88px;height:31px"
                   src="https://jigsaw.w3.org/css-validator/images/vcss"
                   alt="Valid CSS!" />    </a>
          </p>
                </div>
            </div>
        </div>
    </div>
</div>
          <!-- end footer__top -->

    <script src="js/jquery.min.js"></script> <!-- jQuery for Bootstrap's JavaScript plugins -->
    <script src="js/popper.min.js"></script> <!-- Popper tooltip library for Bootstrap -->
    <script src="js/bootstrap.min.js"></script> <!-- Bootstrap framework -->
    <script src="js/jquery.easing.min.js"></script> <!-- jQuery Easing for smooth scrolling between anchors -->
    <script src="js/swiper.min.js"></script> <!-- Swiper for image and text sliders -->
    <script src="js/jquery.magnific-popup.js"></script> <!-- Magnific Popup for lightboxes -->
    <script src="js/morphext.min.js"></script> <!-- Morphtext rotating text in the header -->
    <script src="js/isotope.pkgd.min.js"></script> <!-- Isotope for filter -->
    <script src="js/validator.min.js"></script> <!-- Validator.js - Bootstrap plugin that validates forms -->
    <script src="js/scripts.js"></script> <!-- Custom scripts -->
</body>
</html>

Line 127 says <form id="form1" method="POST" action="/"> which means to post the form data to the homepage of your site, which means the form isn't even working at all. The action should be action="contact.php" instead.

Thanks for that. All fixed.

I'm marking this question as solved. In the future, click the Question Solved toggle :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.