Site icon Hip-Hop Website Design and Development

Add Server Side validation in Ajax mail form

I’m building a form that takes fields values and send them via wp_mail to the post author.
It works, but I want to add a server side validation to improve security.
I don’t know where to add my validation! I want to block the wp_mail command and send out a specific error message.
Here’s my code.

HTML:

<form id="prod_form" action="" method="post">
       <div id="form_succ"></div>
       <div id="form_err"></div>

       <input type="hidden" name="action" value="form_submission">

       <label for="nome">Nome</label>
       <input type="text" id="nome" name="nome" placeholder="Nome">

       <label for="cognome">Cognome</label>
       <input type="text" id="cognome" name="cognome" placeholder="Cognome">

       <label for="email">Email*</label>
       <input type="email" id="email" name="email" placeholder="Email*">

       <label for="telefono">Telefono</label>
       <input type="tel" id="telefono" name="telefono" placeholder="Telefono">

       <label for="numero">Quantità*</label>
       <input type="number" id="numero" name="numero" placeholder="Quantità*">

       <label for="messaggio">Vuoi aggiungere un messaggio?</label>
       <textarea id="messaggio" name="messaggio" placeholder="Vuoi aggiungere un messaggio?"></textarea>

        <div id="prenota"><h4>Invia</h4></div>
        
        <p id="prod_mail"><?php echo get_the_author_meta( 'email', $author_id );?></p>
</form>

IN FUNCTIONS.PHP:


function smail(){

    $nome = $_REQUEST['nome'];
    $cognome = $_REQUEST['cognome'];
    $email = $_REQUEST['email'];
    $telefono = $_REQUEST['telefono'];
    $numero = $_REQUEST['numero'];
    $messaggio= $_REQUEST['messaggio'];
    $prod_mail = $_REQUEST['prod_mail'];
    $prodotto = $_REQUEST['prodotto'];
    $confezione = $_REQUEST['confezione'];
    $prezzo = $_REQUEST['prezzo'];

    if($nome === '') {
        $nome = "<span style='color:#8c8c8c;'>Nome non inserito</span>";
    };
    if($cognome === '') {
        $cognome = "<span style='color:#8c8c8c;'>Cognome non inserito</span>";
    };
    if($telefono === '') {
        $telefono = "<span style='color:#8c8c8c;'>Telefono non inserito</span>";
    };
    if($messaggio === '') {
        $messaggio = "<span style='color:#8c8c8c;'>Nessun messaggio</span>";
    };

    $subject = "Ordine dal sito";
    $email_body = "<h3>Hai ricevuto un nuovo ordine</h3>
        <h4>Prodotto</h4>
        <p>Prodotto: " . $prodotto . "<br>
        Confezione: " . $confezione . "<br>
        Prezzo unitario: " . $prezzo . "<br>
        Quantità: " . $numero . "
        </p>

        <h4>Cliente</h4>
        <p>Nome: ". $nome . "<br>
        Cognome: " . $cognome . "<br>
        Email: " . $email . "<br>
        Telefono: " . $telefono . "
        </p>

        <p><strong>Messaggio</strong><br>
        " . $messaggio. "
        </p>

        <p>
        Contatta il cliente al suo indirizzo email per procedere con l'ordine:<strong> <a href='mailto:" . $email . "'>" . $email . "</a></strong>
        </p>";

        $to = $prod_mail;
        $headers = array('Content-Type: text/html; charset=UTF-8;');

    wp_mail($to, $subject, $email_body, $headers);
    die();
}

add_action( 'wp_ajax_smail', 'smail' );
add_action( 'wp_ajax_nopriv_smail', 'smail' );

JS:


jQuery(function ($) {

$('#prenota').bind('click', function() {
    
        var nome = $("#nome").val();
        var cognome = $("#cognome").val();
        var email = $("#email").val();
        var telefono = $("#telefono").val();
        var numero = $("#numero").val();
        var messaggio = $("#messaggio").val();
        var prod_mail = $("#prod_mail").html();
        var prodotto = $("#prodotto").html();
        var confezione = $("#confezione").html();
        var prezzo = $("#prezzo").html();

        $.ajax({
            url: avars.ajaxurl,
            data: {
                action: 'smail',
                'nome': nome,
                'cognome': cognome,
                'email': email,
                'telefono': telefono,
                'numero': numero,
                'messaggio': messaggio,
                'prod_mail': prod_mail,
                'prodotto': prodotto,
                'confezione': confezione,
                'prezzo': prezzo,
                },
            success: function() {
                $("#prod_form")[0].reset();
                $("#form_succ").html("<p>Ordine inviato! Il produttore ti contatterà per procedere all'acquisto.</p>");
            }
        });

});

});