形式与验证问题

时间:2014-04-24 16:04:31

标签: php html html5

似乎无法让我的表单上的验证码尝试各种方式,但可能是简单的

contact_form.php

<?php
if( isset($_POST) ){

    //form validation vars
    $formok = true;
    $errors = array();

    //submission data
    $ipaddress = $_SERVER['REMOTE_ADDR'];
    $date = date('d/m/Y');
    $time = date('H:i:s');

    //form data
    $name = $_POST['name'];    
    $email = $_POST['email'];
    $telephone = $_POST['telephone'];
    $enquiry = $_POST['enquiry'];
    $message = $_POST['message'];
    $security = $_POST['security'];

    //validate form data
    //validate name is not empty
    if(empty($name)){
        $formok = false;
        $errors[] = "You have not entered a name";
    }

    //validate email address is not empty
    if(empty($email)){
        $formok = false;
        $errors[] = "You have not entered an email address";
    //validate email address is valid
    }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $formok = false;
        $errors[] = "You have not entered a valid email address";
    }

    //validate message is not empty
    if(empty($message)){
        $formok = false;
        $errors[] = "You have not entered a message";
    }
    //validate message is greater than 20 characters
    elseif(strlen($message) < 20){
        $formok = false;
        $errors[] = "Your message must be greater than 20 characters";
    }
    //validate captcha
        include_once dirname(__FILE__) . '/securimage/securimage.php';
        $securimage = new Securimage();

    if(empty($security)){
        $formok = false;
        $errors[] = "You have not entered a valid security code";
    }
    elseif ($securimage->check($security) == false){
        $errors[] = "Incorrect security code entered";
    }


    //send email if all is ok
    if($formok){
        $headers = "From: enquiries@solvadamp-suffolk.co.uk" . "\r\n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

        $emailbody = "<p>You have received a new message from the enquiries form on your website.</p>
                      <p><strong>Name: </strong> {$name} </p>
                      <p><strong>Email Address: </strong> {$email} </p>
                      <p><strong>Telephone: </strong> {$telephone} </p>
                      <p><strong>Enquiry: </strong> {$enquiry} </p>
                      <p><strong>Message: </strong> {$message} </p>
                      <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

        mail("nathan@coopertechnologies.co.uk","New Enquiry",$emailbody,$headers);

    }

    //what we need to return back to our form
    $returndata = array(
        'posted_form_data' => array(
            'name' => $name,
            'email' => $email,
            'telephone' => $telephone,
            'enquiry' => $enquiry,
            'message' => $message,
            'security' => $security
        ),
        'form_ok' => $formok,
        'errors' => $errors
    );


    //if this is not an ajax request
    if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
        //set session variables
        session_start();
        $_SESSION['cf_returndata'] = $returndata;

        //redirect back to form
        header('location: ' . $_SERVER['HTTP_REFERER']);
    }
}
?>

contact.php - 如果您需要将所有错误粘贴起来,请填写

    </header>
        <div id="main-content">
                <div id="contact-form" class="clearfix">
                    <h3>Get In Touch!</h3>
                    <h4>Please provide as much information as possible for us to help you with your enquiry.</h4>
                    <?php
                    //init variables
                    $cf = array();
                    $sr = false;

                    if(isset($_SESSION['cf_returndata'])){
                    $cf = $_SESSION['cf_returndata'];
                    $sr = true;
                    }
                    ?>
                    <ul id="errors" class="<?php echo ($sr && !$cf['form_ok']) ? 'visible' : ''; ?>">
                    <li id="info">There were some problems with your form submission:</li>
                        <?php 
                        if(isset($cf['errors']) && count($cf['errors']) > 0) :
                            foreach($cf['errors'] as $error) :
                        ?>
                        <li><?php echo $error ?></li>
                        <?php
                            endforeach;
                        endif;
                        ?>
                    </ul>
                    <p id="success" class="<?php echo ($sr && $cf['form_ok']) ? 'visible' : ''; ?>">Thanks for your message! We will get back to you as soon as possible!</p>
                    <form method="post" action="contact_form.php">
                    <label for="name">Name: <span class="required">*</span></label>
                    <input type="text" id="name" name="name" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['name'] : '' ?>" placeholder="John Doe" required="required" autofocus="autofocus" />

                    <label for="email">Email Address: <span class="required">*</span></label>
                    <input type="email" id="email" name="email" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['email'] : '' ?>" placeholder="johndoe@example.com" required="required" />

                    <label for="telephone">Telephone: </label>
                    <input type="tel" id="telephone" name="telephone" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['telephone'] : '' ?>" />

                    <label for="enquiry">Enquiry: </label>
                        <select id="enquiry" name="enquiry">
                            <option value="General" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['enquiry'] == 'General') ? "selected='selected'" : '' ?>>General</option>                             
                            <option value="Damp Proofing" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['enquiry'] == 'Damp Proofing') ? "selected='selected'" : '' ?>>Damp Proofing</option>
                            <option value="Basement Tanking" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['enquiry'] == 'Basement Tanking') ? "selected='selected'" : '' ?>>Basement Tanking</option>
                            <option value="Woodworm &amp; Dry-rot" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['enquiry'] == 'Woodworm &amp; Dry-rot') ? "selected='selected'" : '' ?>>Woodworm &amp; Dry-rot</option>
                            <option value="Condensation Remediation" <?php echo ($sr && !$cf['form_ok'] && $cf['posted_form_data']['enquiry'] == 'Condensation Remediation') ? "selected='selected'" : '' ?>>Condensation Remediation</option>
                        </select>

                    <label for="message">Message: <span class="required">*</span></label>
                    <textarea id="message" name="message" placeholder="Your message must be greater than 20 characters" required="required" data-minlength="20"><?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['message'] : '' ?></textarea>

                    <label for="security">Security: <span class="required">*</span></label>
                    <img id="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image" />
                    <input type="text" id="security" name="security" value="<?php echo ($sr && !$cf['form_ok']) ? $cf['posted_form_data']['security'] : '' ?>" size="10" maxlength="6" required="required"/>
                    <a href="#" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false">[ Different Image ]</a><br />

                    <span id="loading"></span>
                    <input type="submit" value="Submit" id="submit-button" />
                    <p id="req-field-desc"><span class="required">*</span> indicates a required field</p>
                </form>
                <?php unset($_SESSION['cf_returndata']); ?>
            </div>              
        </div>
    <footer>

希望有人能够发现我在使用或至少试图使用http://www.phpcaptcha.org/

的验证码时错过了什么

0 个答案:

没有答案