Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    Omettere campi nulli nell' invio della mail di un form.

    Salve. Come posso fare in modo che se l'utente non riempie alcuni campi del form, nella mail inviata salti quel dato nullo e inserisca solo i campi inseriti dall'utente?

    esempio: non viene inserito il campo "Provincia"

    nella mail risulterà:

    Nome: Mario
    Cognome: Rossi
    Nata/o a: Roma
    Il: 25/12/1985
    Via, Piazza, etc..: Via Tal De Tali 45
    Via, Piazza, etc..: Via Tal De Tali 45
    Città: Roma
    CAP: 34666
    Paese: ITALY
    etc; etc....

    Se invece viene inserito nella mail risulterà:

    Nome: Mario
    Cognome: Rossi
    Nata/o a: Roma
    Il: 25/12/1985
    Via, Piazza, etc..: Via Tal De Tali 45
    Via, Piazza, etc..: Via Tal De Tali 45
    Città: Roma
    Provincia: RO
    CAP: 34666
    Paese: ITALY
    etc; etc....



    Questo è tutto il codice php:


    codice:
    <?php
     // L'INDIRIZZO DEL DESTINATARIO DELLA MAIL
     $to = "amivan85@gmail.com";
     
     // IL SOGGETTO DELLA MAIL
     $subject = "Modulo Richiesta informazioni da www.xxx.xxx";
     
     // COSTRUZIONE DEL CORPO DEL MESSAGGIO
     $body = "Contenuto del modulo:\n\n";
     
     $body .= "Nome: " . trim(stripslashes($_POST["nome"])) . "\n";
     $body .= "Cognome: " . trim(stripslashes($_POST["cognome"])) . "\n";
     $body .= "Nata/o a: " . trim(stripslashes($_POST["element_2"])) . "\n";
     $body .= "Il: " . trim(stripslashes($_POST["element_3_2"])) . "/" . trim(stripslashes($_POST['element_3_1'])) . "/" . trim(stripslashes($_POST['element_3_3'])) . "\n";
     
     
     $body .= "Indirizzo\n\n";
     
     $body .= "Via, Piazza, etc..: " . trim(stripslashes($_POST["element_4_1"])) . "\n";
     $body .= "Via, Piazza, etc..: " . trim(stripslashes($_POST["element_4_2"])) . "\n";
     $body .= "Città: " . trim(stripslashes($_POST["element_4_3"])) . "\n";
     $body .= "Provincia: " . trim(stripslashes($_POST["element_4_4"])) . "\n";
     $body .= "CAP: " . trim(stripslashes($_POST["element_4_5"])) . "\n";
     $body .= "Paese: " . trim(stripslashes($_POST["element_4_6"])) . "\n";
     
     
     $body .= "Telefono: " . trim(stripslashes($_POST["telefono"])) . "-" . trim(stripslashes($_POST["telefono2"])) . "-" . trim(stripslashes($_POST["telefono3"])) . "\n";
     $body .= "Mail: " . trim(stripslashes($_POST["element_6_a"])) . "\n";
     
     
     $body .= "Il mio livello di ballo è!: " . trim(stripslashes($_POST["element_9_a"])) . "\n";
     $body .= "Vorrei imparare questi balli: ";
     $body .= trim(stripslashes($_POST["element_8_1"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_2"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_3"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_4"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_5"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_6"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_7"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_8"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_9"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_10"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_11"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_12"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_13"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_14"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_15"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_16"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_17"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_18"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_19"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_20"])) . "\n";
     $body .= trim(stripslashes($_POST["element_8_21"])) . "\n";
     $body .= "Altro: " . trim(stripslashes($_POST["element_7"])) . "\n";
     
     
     $body .= "Ho conosciuto il sito tramite...: " . trim(stripslashes($_POST["sito"])) . trim(stripslashes($_POST["sito2"])) . trim(stripslashes($_POST["sito3"])) . trim(stripslashes($_POST["sito4"])) . trim(stripslashes($_POST["sito5"])) . "\n";
     $body .= "Desidero ballare nella scuola di...: " . trim(stripslashes($_POST["element_9"])) . "\n";
     
     
     $body .= "Volevo imparare a ballare da...: " . trim(stripslashes($_POST["element_1"])) . "\n";
     $body .= "Ho deciso proprio adesso di iniziare a ballare perché...: " . trim(stripslashes($_POST["element_2"])) . "\n";
     $body .= "Ho detto che voglio imparare a ballare ha.... E pensano che...: " . trim(stripslashes($_POST["element_3"])) . "\n";
     $body .= "Vorrei venire a ballare questo numero di volte a settimana...: " . trim(stripslashes($_POST["element_4"])) . "\n";
     $body .= "Mi aspetto da questa esperienza di...: " . trim(stripslashes($_POST["element_5"])) . "\n";
     $body .= "Ballerò (Da solo o con qualcuno)...: " . trim(stripslashes($_POST["element_6"])) . "\n";
     
     
     // INTESTAZIONI SUPPLEMENTARI
     $headers = "From: Modulo Richiesta informazioni. <info@xxx.xxx>";
     
     // INVIO DELLA MAIL
     if(@mail($to, $subject, $body, $headers)) { 
    
     echo "La mail è stata inoltrata con successo.";
     
     } else {
     
     echo "Si sono verificati dei problemi nell'invio della mail.";
     
     }
     
     ?>
    Provincia: RO Provincia: RO

  2. #2
    Ti basta controllare con un semplice IF se quel campo è valorizzato a meno prima di concatenarlo agli altri:

    Una cosa del tipo (applicata solo alla provincia ad esempio):

    Codice PHP:
    ...
    $body .= "Città: " trim(stripslashes($_POST["element_4_3"])) . "\n";
    if (isset(
    $_POST["element_4_4"])) {
        
    $body .= "Provincia: " trim(stripslashes($_POST["element_4_4"])) . "\n";
    }
    $body .= "CAP: " trim(stripslashes($_POST["element_4_5"])) . "\n";
    ... 
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  3. #3
    però la scritta provincia appare comunque.
    ce modo di annullare anche quel campo?

  4. #4
    Originariamente inviato da amivan85
    però la scritta provincia appare comunque.
    ce modo di annullare anche quel campo?
    Come fa a comparire la scritta se la sua concatenazione si trova dentro all'IF?!?!?!
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  5. #5
    esatto quello che mi chiedo anche io

    esempio:


    codice:
     if (isset($_POST["element_2"])) { 
     	$body .= "Nata/o a: " . trim(stripslashes($_POST["element_2"])) . "\n";
     }
     
     if (isset($_POST["element_3_2"]) && isset($_POST["element_3_1"]) && isset($_POST["element_3_3"])) { 
     	$body .= "Il: " . trim(stripslashes($_POST["element_3_2"])) . "/" . trim(stripslashes($_POST['element_3_1'])) . "/" . trim(stripslashes($_POST['element_3_3'])) . "\n";
     	}

    In questo caso se io non metto niente in questi campi, nella mail mi viene la scritta "Nata/o a: " e "Il: "

    ma il codice mi sembra corretto...

  6. #6
    Evidentemente le variabili $_POST["element_2"] e $_POST["element_3_2"] non sono null, ma contengono spazi oppure la stringa vuota "" per cui il controllo isset() non basta. Prova ad utilizzare anche empty()...un po' di iniziativa dai!

    Codice PHP:
    if (isset($_POST["element_2"]) && !empty($_POST["element_2"])) { 
        ... 
    Io ti ho dato lo spunto per capire come fare, poi mettici del tuo.
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  7. #7
    adesso funziona.
    Purtroppo non conosco questo linguaggio e quindi non ci arrivo subito alle cose.
    Ma imparerò
    Grazie mille per l'aiuto

  8. #8
    Probabilmente basta anche solo il controllo fatto con la funzione empty(). Puoi rimuovere l'isset().

    Codice PHP:
    if (!empty($_POST["element_2"])) { 
        ... 
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

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 © 2025 vBulletin Solutions, Inc. All rights reserved.