0

on my site i need to have a mandatory checkbox in the contact form? I need it for EU-compliance, so the form could not be sent if the "terms & conditions" are not be accepted..

Here my html code:

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

And this is the .php file

<?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);
}
}
?>
  • Yes, that's your code. Now what is wrong with it and what have you done to try and diagnose the problem? – Mike Cluck Nov 21 '16 at 21:40
  • I tried with – Antonio C. Nov 21 '16 at 21:41
  • So what? Are you saying the form is being submitted even if they don't agree to the terms? What does [your console](http://stackoverflow.com/documentation/javascript/185/hello-world/714/using-console-log) say? Have you searched for any other questions asking how to prevent a form from submitting? – Mike Cluck Nov 21 '16 at 21:42
  • Exactly.. after that the alert appear, the form is being submitted even if they don't agree the t&c.. – Antonio C. Nov 21 '16 at 21:44
  • [This will be useful.](http://stackoverflow.com/questions/3350247/how-to-prevent-form-from-being-submitted) I'd also suggest not mixing your JS into your HTML. Trust me, you're going to add a bit of code to that and writing it all in HTML is a huge pain. – Mike Cluck Nov 21 '16 at 21:47
  • Thanks! I'll try it! – Antonio C. Nov 21 '16 at 21:56

0 Answers0