• User Attivo

    Problema javasript form di registrazione e invio mail php

    Ciao a tutti,

    Ho un modulo in html con 3 campi, i cui valori sono spediti a un javascript, che dopo aver controllato i campi fa venire fuori messaggi di errore o successo sull'Html e invia i dati a un php che li spedisce con la funziona mail().

    Purtroppo però, non funziona..non sono un programmatore js/php e non riesco a capire cosa ci sia di sbagliato.

    Posto il codice qui sotto, chi mi può aiutare cosa c'è di sbagliato?

    HTML (index.html)

    <form class="registration-form" id="register" role="form">

                    <!-- IF MAIL SENT SUCCESSFULLY -->
                    <h6 class="registration-form success-reg">
                    <span class="colored-text icon_check"></span> Grazie per averci scritto!</h6>
                    
                    <!-- IF MAIL SENDING UNSUCCESSFULL -->
                    <h6 class="registration-form error-reg">
                    <span class="colored-text icon_error-circle_alt"></span> :-( Errore: Riprova!</h6>
                        
                        <input class="form-control input-box" id="regname" type="text" name="regname" placeholder="Inserisci il tuo nome">
                        <input class="form-control input-box" id="regemail" type="email" name="regemail" placeholder="Inserisci la tua email">
                        <textarea class="form-control textarea-box" id="regrequest" type="text" name="regrequest" placeholder="Inserisci la tua richiesta"></textarea>
                        <button class="btn standard-button" type="submit" id="regsubmit" name="regsubmit">Invia!</button>
    
    
                    </form>
    

    JAVASCRIPT (custom.js)

    $("#register").submit(function (e) {
    e.preventDefault();
    var rname = $("#regname").val();
    var remail = $("#regemail").val();
    var rrequest = $("#regrequest").val();
    var dataString = 'rname=' + rname + '&remail=' + remail + '&rrequest=' + rrequest;
    //document.write(dataString);

        function isValidEmail(emailAddress) {
            var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
            return pattern.test(emailAddress);
        };
        if (isValidEmail(remail) && (rrequest.length > 1) && (rname.length > 1)) {
        //document.write(dataString);
            $.ajax({
                type: "POST",
                url: "sendmail-reg.php",
                data: dataString,
                success: function () {
                    $('.success-reg').fadeIn(1000);
                    $('.error-reg').fadeOut(500);
                }
            });
        }
        else {
            $('.error-reg').fadeIn(1000);
            $('.success-reg').fadeOut(500);
        }
        //document.write(dataString);
        return false;
    });
    

    **PHP (sendmail-reg.php)

    **<?php
    // Email Submit
    // Note: filter_var() requires PHP >= 5.2.0

    if ( isset($_POST['remail']) && isset($_POST['rname']) && isset($_POST['rrequest']) ) && filter_var($_POST['remail'], FILTER_VALIDATE_EMAIL) ) {

    // detect & prevent header injections
    $test = "/(content-type|bcc:|cc:|to:)/i";
    foreach ( $_POST as $key => $val ) {
    if ( preg_match( $test, $val ) ) {
    exit;
    }
    }

    $headers = 'From: ' . $_POST["rname"] . '<' . $_POST["remail"] . '>' . "\r\n" .
    'Reply-To: ' . $_POST["remail"] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    $subject = "Preventivo traduzione sito web";

    mail( "[email protected]", $subject, $_POST['rrequest'], $headers );

    // ^
    // Replace with your email
    }

    ?>

    Grazie mille!