PHP表单不在窗体上显示错误消息

时间:2016-09-29 18:47:45

标签: php html5

我正在尝试修复其他人创建的网站,并且我试图在表单上显示验证错误。我已经尝试过高低搜索,无法让它发挥作用。例如,如果电话号码少于2个字符,它将在表单上显示错误消息,而不是弹出窗口。如果一切都通过验证,表格将通过电子邮件发送。

目前,问题是如果有人输入的信息未通过验证,则不会显示任何错误,客户认为他们向我们发送了电子邮件。

这是联系表格:

<?php
ini_set('display_errors', 1);
session_start();
include("captcha.php");
$_SESSION['captcha'] = simple_php_captcha();

if($_POST){
    include("contact_process.php");
}
?>
<!DOCTYPE html><html hola_ext_inject="disabled">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="robots" content="index, follow" />
    <title>Test</title>
    <!-- Mobile Meta -->
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <!-- Put favicon.ico and apple-touch-icon(s).png in the images folder -->
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
    <link rel="apple-touch-icon" href="images/apple-touch-icon.png" />
    <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png" />
    <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png" />
    <!-- CSS StyleSheets -->
    <link href="http://fonts.googleapis.com/css?family=Oswald:400,300,700" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="/css/font-awesome.min.css" />
    <link rel="stylesheet" href="/css/animate.css" />
    <link rel="stylesheet" href="/css/prettyPhoto.css" />
    <link rel="stylesheet" href="/css/slick.css" />
    <link rel="stylesheet" href="/rs-plugin/css/settings.css" />
    <link rel="stylesheet" href="/css/style.css" />
    <link rel="stylesheet" href="/css/responsive.css" />
    <!--[if lt IE 9]>
        <link rel="stylesheet" href="css/ie.css">
        <script type="text/javascript" src="js/html5.js"></script>
    <![endif]-->
    <link rel="stylesheet" href="/css/skins/default.css" />
</head>
<body>
    <!-- site preloader start -->
    <div class="page-loader">
    =
    <div class="loader-in"></div>
    </div>
    <!-- site preloader end -->
    <div class="pageWrapper">
    <!-- Header Start -->
    <div id="headWrapper" class="clearfix">
    <!-- top bar start -->
    <div class="top-bar">
    <div class="container">
    <div class="row">
    <div class="cell-5">
    </div>
    </div>
    </div>
    </div>
    <!-- top bar end -->
    <!-- Logo, global navigation menu and search start -->
    <header class="top-head">
    <div class="container">
    <div class="row">
    <div class="logo cell-3">
    <a href="/index.html"></a>
    </div>
    <div class="top-logo-headline">
    <headline>
    Test
    </headline>
    </div>
    <div class="cell-5 top-menu">
    <!-- top navigation menu start -->
    <nav class="top-nav">
    </nav>
    <!-- top navigation menu end -->
    </div>
    </div>
    </div>
    </header>

        </div>
        <!-- Header End -->

        <!-- Content Start -->
        <div id="contentWrapper">
            <div class="page-title title-1">
                <div class="container">
                    <div class="row">
                        <div class="cell-12">
                            <h1 class="fx" data-animate="fadeInLeft">Contact <span>us</span></h1>

                        </div>
                    </div>
                </div>
            </div>

            <div class="padd-top-50">
                <div class="container">
                    <div class="row">
                        <div class="cell-7 contact-form fx" data-animate="fadeInLeft" id="contact">
                            <h3 class="block-head"></h3>
                            <div id="message" style="display: none;">

                                <p class="congrats">Thank You!</p>
                                <p class="congratsTxt">Your Message was successfully Sent , please click the link below to go to home page.</p>
                                <div><a href="/index.html" class="btn btn-large main-bg">Go to home page</a></div>
                            </div>
                            <form class="form-signin cform" method="post" action="contact_process.php" id="cform" autocomplete="on">
                                <div class="form-input">
                                    <label>First name<span class="red">*</span></label>
                                    <input type="text" name="name" id="name" required>
                                </div>
                                    <div class="form-input">
                                    <label>Company name<span class="red">*</span></label>
                                    <input type="text" name="company" id="company" required>
                                </div>
                                <div class="form-input">
                                    <label>Email<span class="red">*</span></label>
                                    <input name="email" type="email" id="email" required>
                                </div>
                                <div class="form-input">
                                    <label>Phone<span class="red">*</span></label>
                                    <input name="phone" type="text" id="phone" required>
                                </div>
                                <div class="form-input">
                                    <label>Message<span class="red">*</span></label>
                                    <textarea name="message" cols="40" rows="7" id="messageTxt" spellcheck="true" required></textarea>
                                </div>

                                <div class="form-input">
                              <h4 class="block-head">Direct Message To: </h4>
                                <select name="to">
                                <option value="0">Sales (product information, price inquiries, quotes)</option>
                                <option value="1">Tech Support (troubleshoot hardware or software issues)</option>
                                <option value="2">Accounting (billing questions, A/P, A/R)</option>
                                <option value="3">Engineering (application engineering support)</option>
                                <option value="4">Marketing (contact the Marketing department)</option>
                                <option value="5">Order Inquiry (check existing order status)</option>
                                </select> </div>

                                <div  class="form-input">
                                    <div class="row">
                                        <div class="cell-12 margin-bottom-20"><img alt="" src="<?php echo $_SESSION['captcha']['image_src']; ?>" id='captchaimg' ></div>
                                        <input type="hidden" id="hidCap" value="<?php echo $_SESSION['captcha']['code']; ?>" data-error="The captcha code does not match!" />
                                        <div class="cell-3"><input id="captcha_input" name="captcha_input" type="text" required></div>
                                        <div class="cell-6"><label for='message'>Enter the code above :</label><small>Can't read the image? click here to <a href='#' id="rfrshCaptcha">refresh</a></small></div>
                                    </div>
                                </div>
                                <div class="form-input">
                                    <input id="submit" name="submit" type="submit" class="btn btn-large main-bg" value="Submit">&nbsp;&nbsp;<input type="reset" class="btn btn-large" value="Reset" id="reset">
                                </div>

                            </form>
                        </div>
                        <div class="cell-5 contact-detalis">
    <div class="padd-vertical-45">
    <div class="container">
    <div class="row">
    </div>
    </div>
    </div>
    </div>
    </div>
    <!-- Content End -->

        <!-- Footer start -->
    <footer id="footWrapper">
    <div class="footer-top footer-top-2">
    <div class="container">
    <div class="row">
    <!-- footer cell 1 start -->
    <div class="cell-2">
    </div>
    <!-- footer cell 1 start -->
    <!-- main menu footer cell start -->
    <div class="cell-3">
    <h3 class="block-head">Main Menu</h3>
    <ul class="footer-menu">
        <li><a href="/index.html">Home</a></li>
        <li><a href="/product-categories.html">Products</a></li>
        <li><a href="/support.html">Support</a></li>
        <li><a href="/php/contact.php">Contact Us</a></li>
        <li><a href="/site-map/sitemap.html">Site Map</a></li>
    </ul>
    </div>
    <!-- main menu footer cell start -->
    <!-- Useful Links footer cell start -->
    <div class="cell-3">
    <h3 class="block-head">Useful Links</h3>
    </div>
    <!-- Useful Links footer cell start -->
    <!-- contact us footer cell start -->
    <div class="cell-3">
    <h3 class="block-head">Keep in Touch</h3>
    </div>
    <!-- contact us footer cell end -->
    </div>
    </div>
    </div>
    <!-- footer bottom bar start -->
    <div class="footer-bottom">
    <div class="container">
    <div class="row">
    <!-- footer copyrights left cell -->
    </div>
    </div>
    <!-- footer bottom bar end -->
    </footer>
    <!-- Footer end -->

        <!-- Back to top Link -->
        <div id="to-top" class="main-bg"><span class="fa fa-chevron-up"></span></div>

    </div>

    <div class="ModalPopUp">
        <div class="ModalContainer">
            <div class="Modalheader">
                <h3></h3>
                <a href="#" class="closePopup"><span class="fa fa-times"></span></a>
            </div>
            <div class="ModalContent">
            </div>
        </div>
    </div>
    <!-- Load JS siles -->      <script type="text/javascript" src="/js/jquery.min.js"></script>

    <!-- Waypoints script -->
    <script type="text/javascript" src="/js/waypoints.min.js"></script>

    <!-- SLIDER REVOLUTION SCRIPTS  -->
    <script type="text/javascript" src="/rs-plugin/js/jquery.themepunch.tools.min.js"></script>
    <script type="text/javascript" src="/rs-plugin/js/jquery.themepunch.revolution.min.js"></script>

    <!-- Animate numbers increment -->
    <script type="text/javascript" src="/js/jquery.animateNumber.min.js"></script>

    <!-- slick slider carousel -->
    <script type="text/javascript" src="/js/slick.min.js"></script>

    <!-- Animate numbers increment -->
    <script type="text/javascript" src="/js/jquery.easypiechart.min.js"></script>

    <!-- PrettyPhoto script -->
    <script type="text/javascript" src="/js/jquery.prettyPhoto.js"></script>

    <!-- Share post plugin script -->
    <script type="text/javascript" src="/js/jquery.sharrre.min.js"></script>

    <!-- Product images zoom plugin -->
    <script type="text/javascript" src="/js/jquery.elevateZoom-3.0.8.min.js"></script>

    <!-- Input placeholder plugin -->
    <script type="text/javascript" src="/js/jquery.placeholder.js"></script>

    <!-- Contact us js file -->
    <script type="text/javascript" src="/js/contact.js"></script>

    <!-- Tweeter API plugin -->
    <script type="text/javascript" src="/js/twitterfeed.js"></script>

    <!-- Tweeter API plugin -->
    <script type="text/javascript" src="/js/jflickrfeed.min.js"></script>

    <!-- NiceScroll plugin -->
    <script type="text/javascript" src="/js/jquery.nicescroll.min.js"></script>

    <!-- jquery cookie script file -->
    <script type="text/javascript" src="/js/jquery.cookie.js"></script><script type="text/javascript" src="js/mailChimp.js"></script>

    <!-- Style script file -->
    <script type="text/javascript" src="/js/jquery.style.js"></script>

    <!-- general script file -->
    <script type="text/javascript" src="/js/script.js"></script>

    <!-- TopSpot Footer Code - START -->
    <script type="text/javascript" src="../js/topspot-footer.js"></script>
    <!-- TopSpot Footer Code - END -->

    <script>

        $('#rfrshCaptcha').click(function(e){
            $.get('refresh.php', function(url) {
                var code = url.split('&&');
                $('#captchaimg').attr('src', code[0]);
            $('#hidCap').val(code[1]);
            });
            e.preventDefault()
            return false;
        });

    </script>
    <script type="text/javascript">
document.write(unescape("%3Cscript src='" + document.location.protocol + "//www.webtraxs.com/trxscript.php' type='text/javascript'%3E%3C/script%3E"));
</script>

</body>
</html>

这是contact_process表单:

<?php

ini_set('display_errors', 1);
session_start();


Class ContactClass {

    // override the ip address of the API call.
    private $client_ip;

    function getIP()
    {
      if($this->client_ip) 
      {
          return $this->client_ip;
      }
      elseif (!empty($_SERVER['HTTP_CLIENT_IP']))
      {
          return $_SERVER['HTTP_CLIENT_IP'];
      } 
      elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
      {
          return $_SERVER['HTTP_X_FORWARDED_FOR'];
      }
      else
      {
          return $_SERVER['REMOTE_ADDR'];
      }
    }
}

// Initialize class for functions
$contact = new ContactClass();
//echo $contact->getIP();

$site_name = "Test";
if($_POST){
    $name=$_POST['name'];
    $company=$_POST['company'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $message=$_POST['message'];

    if(isset($_POST['email'])) {
        $email_to = "email@your-domain.com";
        $email_subject = "Contact Us Email from : ".$site_name;
        function died($error) {
            echo "We are very sorry, but there were error(s) found with the form you submitted. ";
            echo "These errors appear below.<br /><br />";
            echo $error."<br /><br />";
            echo "Please go back and fix these errors.<br /><br />";
            die();
        }
        if(!isset($_POST['name']) ||
            !isset($_POST['company']) ||
            !isset($_POST['email']) ||
            !isset($_POST['phone']) ||
            !isset($_POST['message'])) {
            die('We are sorry, but there appears to be a problem with the form you submitted.');
        }
        if( empty($_SESSION['captcha']['code'] ) ||
            strcasecmp($_SESSION['captcha']['code'], $_POST['captcha_input']) != 0 ){
            die(' The captcha code does not match! ');
        }
        $name = $_POST['name']; // required
        $company = $_POST['company']; // required
        $email_from = $_POST['email']; // required
        $phone = $_POST['phone']; // required
        $message = $_POST['message']; // required
        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
        if(!preg_match($email_exp,$email_from)) {
            $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
        }
        $string_exp = "/^[A-Za-z .'-]+$/";
        if(!preg_match($string_exp,$name)) {
            $error_message .= 'The Name you entered does not appear to be valid.<br />';
        }
        if(strlen($company) < 2) {
            $error_message .= 'The company name you entered does not appear to be valid.<br />';
        }
        if(strlen($phone) < 7) {
            $error_message .= 'The phone number you entered does not appear to be valid.<br />';
        }
        if(strlen($message) < 2) {
            $error_message .= 'The message you entered does not appear to be valid.<br />';
        }
        if(strlen($error_message) > 0) {
            die($error_message);
        }

        $email_routing =    array(
                           'email1@domain.com',
                             'email2@domain.com',
                             'email3@domain.com',
                             'email4@domain.com',
                             'email5@domain.com',
                             'email6@domain.com'
                        );
        $email_to = $email_routing[$_POST['to']];

        print_r($_POST);
        //die;
        //$email_message = '<html><body><div>';
        $email_message = "Contact us message details below: \n\n";
        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($name)."\n";
        $email_message .= "Company: ".clean_string($company)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Phone: ".clean_string($phone)."\n";
        $email_message .= "Message: ".clean_string($message)."\n";
        $email_message .= 'Senders IP: <a href="http://whatismyipaddress.com/ip/'. $contact->getIP() .'">'. $contact->getIP() .'</a>'."\n";

        //$email_message .='</div></body></html>';
        // email header
        $headers = 'From: webmaster@domain.com' ."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
        @mail($email_to, $email_subject, $email_message, $headers);
        $result=1;

        if($result){
            echo "Thanks ".clean_string($name)." We'll get back to you soon.";
        }
    }

    exit();
}
?>

这件事搞砸了,我迷路了!非常感谢!

0 个答案:

没有答案