• User Newbie

    Problema Checkbox Privacy Contact form - Aiuto!!

    Salve a tutti, vi espongo il mio problema e sono certo che riuscirete a darmi una mano,
    allora, ho il seguente contact form, tutto funziona benissimo solo che ho voluto implementare il checkbox per la privacy.
    adesso il problema è che il checkbox non mi manda i dati nella mail, praticamente quando mi arriva la mail tutti i dati arrivano correttamente, nome, email, testo ecc.. ma vorrei che il checkbox mi mandasse il valore "accetto" se spuntato, in caso contrario "non accetto".
    ho provato ad implementare nel file php vari codici per il checkbox ma purtroppo non sono riuscito a far funzionare niente, praticamente sto impazzendo da giorni, aiutatemi per favore, consigliatemi il codice da aggiungere per poter ricevere i valori del checkbox.
    vi ringrazio in anticipo

    qui il codice html

    [HTML]<form id="contact-form" method="post" action="process.php">
    <div class="form-div-1 element">
    <label>
    <input type="text" placeholder="Nome: *(obbligatorio)" name="name" class="text" /></label>
    </div>
    <div class="form-div-2 element">
    <label>
    <input type="text" placeholder="Email: *(obbligatorio)" name="email" class="text" /></label>
    </div>
    <div class="form-div-3 element">
    <label>
    input type="text" placeholder="Telefono:" name="website" class="text" /></label>
    </div>
    <div class="element1">
    <label class="message">
    <textarea name="comment" placeholder="Messaggio: *(obbligatorio)" class="text textarea" /></textarea>
    </label>
    </div>
    <div class="form-div-1 element">
    <label>
    <input type="checkbox" id="privacy" name="privacy" value="si" />
    </label>
    <p>Accetto <a href="#" title="leggi la nostra informativa" class="informativa_link">l'informativa sulla privacy</a><br /></p>
    </div>
    <div class="element">
    <input type="submit" value="CONTATTACI" id="submit"/>
    </div>
    </form>[/HTML]

    il codice javascript

    [HTML]<script type="text/javascript">$(document).ready(function() {

    //if submit button is clicked
    $('#submit').click(function () {

    //Get the data from all the fields
    var name = $('input[name=name]');
    var email = $('input[name=email]');
    var website = $('input[name=website]');
    var comment = $('textarea[name=comment]');
    var privacy = $('input[name=privacy]');

    //Simple validation to make sure user entered something
    //If error found, add hightlight class to the text field
    if (name.val()=='') {
    name.addClass('hightlight');
    return false;
    } else name.removeClass('hightlight');

    if (email.val()=='') {
    email.addClass('hightlight');
    return false;
    } else email.removeClass('hightlight');

    if (comment.val()=='') {
    comment.addClass('hightlight');
    return false;
    } else comment.removeClass('hightlight');

    //organize the data properly
    var data = 'name=' + name.val() + '&email=' + email.val() + '&website=' + website.val() + '&comment=' + encodeURIComponent(comment.val());

    //disabled all the text fields
    $('.text').attr('disabled','true');

    //show the loading sign
    $('.loading').show();

    //start the ajax
    $.ajax({
    //this is the php file that processes the data and send mail
    url: "process.php",
    //GET method is used
    type: "GET",
    //pass the data
    data: data,
    //Do not cache the page
    cache: false,
    //success
    success: function (html) {
    //if process.php returned 1/true (send mail success)
    if (html==1) {
    //hide the form
    $('.form').fadeOut('slow');
    //show the success message
    $('.done').fadeIn('slow');

    //if process.php returned 0/false (send mail failed)
    } else alert('Sorry, unexpected error. Please try again later.');
    }
    });

    //cancel the submit button default behaviours
    return false;
    });
    });
    </script>[/HTML]

    Adesso di seguito invece il file process.php

    [PHP]<?php
    //Retrieve form data.
    //GET - user submitted data using AJAX//POST - in case user does not support javascript, we'll use POST instead
    $name = ($_GET['name']) ? $_GET['name'] : $_POST['name'];
    $email = ($_GET['email']) ?$_GET['email'] : $_POST['email'];
    $website = ($_GET['website']) ?$_GET['website'] : $_POST['website'];
    $comment = ($_GET['comment']) ?$_GET['comment'] : $_POST['comment'];
    if (!empty($_POST['privacy'])) {
    $privacy="accetto";
    }
    else{
    $privacy="non accetto";
    }
    //flag to indicate which method it uses. If POST set it to 1
    if ($_POST) $post=1;
    //Simple server side validation for POST data, of course, you should validate the email
    if (!$name) $errors[count($errors)] = '(obbligatorio) Inserisci il tuo Nome.';
    if (!$email) $errors[count($errors)] = '(obbligatorio) Inserisci la tua Email.';
    if (!$comment) $errors[count($errors)] = '(obbligatorio) Scrivi un Messaggio.';
    //if the errors array is empty, send the mailif (!$errors) {
    //recipient
    $to = 'Your Name [email protected]';
    //sender
    $from = $name . ' <' . $email . '>';
    //subject and the html message
    $subject = 'Contact form - Messaggio da ' .
    $name;
    $message = '

    <head>
    </head>
    <body>
    <table>
    <tr><td>Name</td><td>' . $name . '</td></tr>
    <tr><td>Email</td><td>' . $email . '</td></tr>
    <tr><td>Website</td><td>' . $website . '</td></tr>
    <tr><td>Privacy</td><td>' .$privacy. '</td></tr>
    <tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr>
    </table>
    </body>
    </html>';
    //send the mail
    $result = sendmail($to, $subject, $message, $from);

    //if POST was used, display the message straight away
    if ($_POST) {
    if ($result) echo 'Messaggio Inviato! Risponderemo al più presto. Grazie.';else echo 'Spiecenti, Si è verificato un errore. Riprova più tardi.';
    //else if GET was used, return the boolean value so that
    //ajax script can react accordingly
    //1 means success, 0 means failed}
    else
    {echo $result;
    }

    //if the errors array has values}
    else {
    //display the errors message
    for ($i=0; $i<count($errors); $i++) echo $errors* . '<br/>';
    echo '<a href="form.php">Back</a>';
    exit;}

    //Simple mail function with HTML headerfunction sendmail ($to, $subject, $message, $from {
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";

    $result = mail($to,$subject,$message,$headers);

    if ($result) return 1;
    else return 0;}
    ?>[/PHP]


  • User Newbie

    Nessuno che mi può aiutare? 😞


  • User Attivo

    A parte che vedo errori nel php di parentesi graffe non aperte o non chiuse, puoi provare così:
    [php]
    if (isset($_POST['privacy'])) {
    $privacy = "accetto";
    } else {
    $privacy = "non accetto";
    }
    [/php]

    Ciao.