Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Errore form Contatti

  1. #1

    Errore form Contatti

    Ciao a tutti di nuovo sto sistemando dei form dei contatti adeguandoli alla obbligatorietà col checkbox privacy e non so come risolvere un errore che mi si presenta in qualunque modalità io modifichi il mio form:

    Ho inserito questo checkbox:

    <input name="privacy" id="privacy" type="radio" value="si" />

    Ora dato che mi sto cervellando vi chiedo per favore come inserisco la condizione IF nel form di seguito e più che altro nella raccolta degli array va aggiunto 'privacy', 'privacy' =>

    Grazie fin da ora.

    <?php
    /*
    * CONFIGURE EVERYTHING HERE
    */


    // an email address that will be in the From field of the email.
    $from = 'Messaggio dal sito <info@lucabarcaccia.it>';


    // an email address that will receive the email with the output of the form
    $sendTo = 'Messaggio dal sito <info@lucabarcaccia.it>';


    // subject of the email
    $subject = 'Messaggio dal sito';


    // form field names and their translations.
    // array variable name => Text to appear in the email
    $fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message');


    // message that will be displayed when everything is OK
    $okMessage = 'Il tuo messaggio è stato inviato correttamente. Verrai contattato al più presto. Grazie!';


    // If something goes wrong, we will display this message.
    $errorMessage = 'There was an error while submitting the form. Please try again later';


    /*
    * LET'S DO THE SENDING
    */


    // if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
    error_reporting(E_ALL & ~E_NOTICE);


    try
    {


    if(count($_POST) == 0) throw new \Exception('Form is empty');

    $emailText = "You have a new message from your contact form\n=============================\n";


    foreach ($_POST as $key => $value) {
    // If the field exists in the $fields array, include it in the email
    if (isset($fields[$key])) {
    $emailText .= "$fields[$key]: $value\n";
    }
    }



    // All the neccessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
    'From: ' . $from,
    'Reply-To: ' . $from,
    'Return-Path: ' . $from,
    );

    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));


    $responseArray = array('type' => 'success', 'message' => $okMessage);
    }
    catch (\Exception $e)
    {
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
    }




    // if requested by AJAX request return JSON response
    if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);


    header('Content-Type: application/json');


    echo $encoded;
    }
    // else just display the message
    else {
    echo $responseArray['message'];
    }


    ?>

  2. #2
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,654
    Ciao, per aggiungere il campo al controllo basta che aggiungi la chiave all'array (sempre se ti serve riportarlo nel testo email, perchè mi pare serva solo a quello, a completare il testo del messaggio finale)
    codice:
    $fields = array(........, 'privacy' => 'Privacy', .........); 
    Ora mi pare che venga solo gestito l'errore sull'invio e nulla riguardo campi obbligatori.
    Puoi aggiungere un controllo sul solo campo e bloccare l'invio se manca appunto privacy.

    A monte, prima del try / catch puoi mettere un controllo bloccante solo per la privacy.

    codice:
    if(!$_POST['privacy']){ //Non selezionata
    echo 'Errore per privacy....';
    return;
    }
    Ad occhio e croce dovrebbe bastare così dal codice che vedo.
    Dalla lista $fields puoi anche non metterlo se non interessa mostrarlo nel testo da inviare.

  3. #3
    Ciao e Grazie mille per la risposta, oggi lo provo, il mio dilemma era dove inserire l'IF.

  4. #4
    Ciao buongiorno, ti volevo dire che il form funziona perfettamente, grazie mille ancora per avermi aiutato. So che era banale, mi rendo conto ma non riuscivo a capire dove inserire la condizione IF.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.