复选框的问题需要php-js

时间:2016-11-21 21:38:37

标签: javascript php checkbox contact-form requiredfieldvalidator

在我的网站上我需要在联系表单中有一个强制性复选框吗? 我需要它符合欧盟标准,因此如果"条款和条款不能发送表格。条件"不被接受..

这是我的HTML代码:

<form class="form contact-form" id="contact_form">
                            <div class="clearfix">

                                <div class="cf-left-col">

                                    <!-- Name -->
                                    <div class="form-group">
                                        <input type="text" name="name" id="name" class="input-md round form-control" placeholder="Nome" pattern=".{3,100}" required>
                                    </div>

                                    <!-- Email -->
                                    <div class="form-group">
                                        <input type="email" name="email" id="email" class="input-md round form-control" placeholder="Email" pattern=".{5,100}" required>
                                    </div>

                                </div>

                                <div class="cf-right-col">

                                    <!-- Message -->
                                    <div class="form-group">                                            
                                        <textarea name="message" id="message" class="input-md round form-control" style="height: 84px;" placeholder="Messaggio"></textarea>
                                    </div>

                                </div>

                            </div>

                            <div class="clearfix">

                                <div class="cf-left-col">

                                    <!-- Inform Tip -->                                        
                                    <div class="form-tip pt-20">
                                        <p class="mt-10"><input type="checkbox" name="checkbox" value="check" required /> Ho preso visione dell'<a href="privacy-cookie-policy.html">informativa sulla privacy</a></p>
                                    </div>

                                </div>

                                <div class="cf-right-col">

                                    <!-- Send Button -->
                                    <div class="align-right pt-10">
                                        <button class="submit_btn btn btn-mod btn-medium btn-round" id="submit_btn" >Invia Messaggio</button>
                                    </div>

                                </div>

                            </div>

                            <div id="result"></div>
                        </form>

这是.php文件

<?php
if($_POST)
{
$to_Email = "antonycee@gmail.com";  address
$subject = 'Messaggio dal form di '.$_SERVER['SERVER_NAME']; 


//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {

    //exit script outputting json data
    $output = json_encode(
    array(
        'type'=>'error', 
        'text' => 'Request must come from Ajax'
    ));

    die($output);
} 

//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userMessage"]))
{
    $output = json_encode(array('type'=>'error', 'text' => 'Input fields are empty!'));
    die($output);
}

//Sanitize input data using PHP filter_var().
$user_Name        = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email       = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Message     = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);

$user_Message = str_replace("\&#39;", "'", $user_Message);
$user_Message = str_replace("&#39;", "'", $user_Message);

//additional php validation
if(strlen($user_Name)<4) // If length is less than 4 it will throw an HTTP error.
{
    $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!'));
    die($output);
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
    $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!'));
    die($output);
}
if(strlen($user_Message)<5) //check emtpy message
{
    $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.'));
    die($output);
}

//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$sentMail = @mail($to_Email, $subject, $user_Message . "\r\n\n"  .'Nome: '.$user_Name. "\r\n" .'Email: '.$user_Email, $headers);

if(!$sentMail)
{
    $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.'));
    die($output);
}else{
    $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_Name .'! Thank you for your email'));
    die($output);
}
}
?>

0 个答案:

没有答案