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:


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

//Validate first
    echo "All fields are mandatory!";
  /*  exit; */

    echo "Bad email value!";
 /*   exit; */

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

$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+)',
  $inject = join('|', $injections);
  $inject = "/$inject/i";
    return true;
    return false;
<!DOCTYPE html>
<html lang="en">

<!--    <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=",700&display=swap&subset=latin-ext" rel="stylesheet" />
    <link href=",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>
    <!-- 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>
    <!-- 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>

    </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>           
                <input type="text" name="name" >

                <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>

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

                <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 />  
    </div>   <!-- end of form class -->
<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>

           <a href="">
              <img style="border:0;width:88px;height:31px"
                   alt="Valid CSS!" />    </a>
          <!-- 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 -->

Recommended Answers

All 3 Replies

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.