2017-05-16 5 views
1

ich in HTML meine Form haben:senden E-Mail mit Hilfe von JavaScript, HTML und Ajax

<form name="form" class="form" role="form" action="index.php" method="post"> 
    <div class="container"> 
    <div class="row" id="row1"> 

     <div class="col-xs-12 col-sm-12 col-md-6 col-lg-4 col-xl-2 col-lg-offset-2 text-center"> 
     <!--<img class="map-class" src="imgs/primaudine_mappa.svg">--> 

     </div> 

     <div class="col-xs-12 col-sm-12 col-md-6 col-lg-4 col-xl-2 form-class"> 
     <div class="row"> 
      <div class="col-sm-12"> 

      <div class="form-group"> 
       <!-- drop down menu --> 
       <label for="sel1">Seleziona Area:</label> 
       <span class="error">* <!--<?php echo $nameErr;?> --> </span> 
       <select class="form-control" id="sel1" name="region" required> 
              <option selected="selected" disabled="disabled" value="">Seleziona la tua zona</option> 
              <option value="region1">1 - Udine Centro</option> 
              <option value="region2">2 - Rizzi/S. Domenico/Cormor/S. Rocco</option> 
              <option value="region3">3 - Laipacco/San Gottardo</option> 
              <option value="region4">4 - Udine sud</option> 
              <option value="region5">5 - Cussignacco</option> 
              <option value="region6">6 - S. Paolo/S. Osvaldo</option> 
              <option value="region7">7 - Chiavris/Paderno</option> 
            </select> 
      </div> 
      </div> 
     </div> 

     <div class="row"> 
      <div class="col-xs-12 col-sm-12 col-md-12"> 
      <div class="form-group"> 
       <label for="usr">Mail personale</label> 
       <span class="error">* <!-- <?php echo $nameErr;?></span> --> 
       <input name="mail" type="email" class="form-control" id="usr" placeholder="es. [email protected]" required> 
      </div> 
      </div> 
     </div> 

     <div class="row"> 

      <div class="col-xs-8 col-sm-8 col-md-8 form-group"> 
      <label for="usr">Nome e Cognome</label> 
      <span class="error">* <!-- <?php echo $nameErr;?></span> --> 
      <!-- codice per nomi e cognomi ^([ \u00c0-\u01ffa-zA-Z'\-])+$ --> 
      <input pattern="^([ \u00c0-\u01ffa-zA-Z'\-])+$" title="Solo caratteri es. Mario Rossi" name="nome" type="text" class="form-control" id="usr" placeholder="es. Mario Rossi" required> 
      </div> 
      <div class="col-xs-4 col-sm-4 col-md-4 form-group"> 
      <label for="usr">Età</label> 
      <span class="error">* <!-- <?php echo $nameErr;?></span> --> 
      <input name="eta" type="number" min="16" max="99" class="form-control" id="usr" placeholder="es. 35" required> 
      </div> 
     </div> 

     <div class="row"> 
      <div class="cols-xs-12 col-sm-12 col-md-12"> 
      <div class="form-group"> 
       <label for="comment">Inserisci la tua segnalazione o i tuoi spunti</label> 
       <span class="error">* <!-- <?php echo $nameErr;?></span> --> 
       <textarea name="commento" class="form-control" rows="7" id="comment" placeholder="Scrivi in questo campo" required></textarea> 
       <p><span class="error">* campi richiesti</span></p> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 
    </div> 

    <div class="container"> 
    <div class="row"> 
     <div class="cols-xs-12 col-sm-12 col-md-12 button-class" style="text-align: center"><button name="submit" type="submit" class="btn" value="submit">Invia Segnalazione</button> 

     </div> 
    </div> 
    </div> 
</form> 

Dann habe ich meine Form in HTML verbunden mit meinem E-Mail-Absender-Code in PHP:

<?php 

      // L'INDIRIZZO DEL DESTINATARIO DELLA MAIL 
      $to = "[email protected]"; 

      // IL SOGGETTO DELLA MAIL 
      $subject = "Segnalazione da " . $_POST["nome"]; 

      // COSTRUIAMO IL CORPO DEL MESSAGGIO 
      $body = "Contenuto della segnalazione:\n\n"; 

      $body .= "Mail: " . trim(stripslashes($_POST["mail"])) . "\n"; 

      $body .= "Nome: " . trim(stripslashes($_POST["nome"])) . "\n"; 

      $body .= "Età: " . trim(stripslashes($_POST["eta"])) . "\n"; 

      $val_select = trim(stripslashes($_POST["region"])); 
      if ($val_select == "region1") { 
       $val_select = "Udine Centro"; 
      } else if ($val_select == "region2") { 
       $val_select = "Rizzi/S. Domenico/Cormor/S. Rocco"; 
      } else if ($val_select == "region3") { 
       $val_select = "Laipacco/San Gottardo"; 
      } else if ($val_select == "region4") { 
       $val_select = "Udine sud"; 
      } else if ($val_select == "region5") { 
       $val_select = "Cussignacco"; 
      } else if ($val_select == "region6") { 
       $val_select = "S. Paolo/S. Osvaldo"; 
      } else if ($val_select == "region7") { 
       $val_select = "Chiavris/Paderno"; 
      } 

      $body .= "Area: " . $val_select . "\n"; 

      $body .= "Messaggio: " . trim(stripslashes($_POST["commento"])) . "\n"; 

      // INTESTAZIONI SUPPLEMENTARI 
      $header_from = "From: " . trim(stripslashes($_POST["nome"])) . " " . trim(stripslashes($_POST["mail"])) . " "; 

      // INVIO DELLA MAIL 
      if($_POST["nome"] != "" && $_POST["commento"] != "" && $_POST["eta"] != "" && $_POST["region"] != ""){ 
       //echo "ciao!"; 
       //mail($to, $subject, $body, $header_from); 
       mail($to, $subject, $body, 'Mail dal sito Prima Udine'); 
       //header("Location:https://www.google.com/"); 
       header("Location: redirect.php"); 


      } 
     ?> 

Aber diese Methode funktioniert nicht. also würde ich gern die Methode ändern. Ich möchte mein Formular in HTML mit einem JavaScript-Code verbinden, der Ajax verwendet, um die E-Mail zu senden. Wie kann ich das tun? Wenn Sie weitere Informationen benötigen, fragen Sie mich, danke. ps. Ich bin Neuling, aber ich kann etwas kodieren;)

UPDATE: Ich habe versucht, das PHP-Problem zu beheben, überprüfen Sie meine Antwort.

+3

Was bedeutet „don 't wor k "gemein? Erhalten Sie einen Fehler? –

+0

Müssen Sie Ihren eigenen Code verwenden? Wenn nein, dann können Sie http://swiftmailer.org/ einen Versuch geben .. – lU5er

+0

@ScottMarcus Nun das einzige, was nicht funktioniert, ist, wenn ich die E-Mail abschicke und ich die Seite aktualisiere, um die E-Mail erneut einzureichen. Ich habe alle Codes und die Lösungen im Web ausprobiert .... es sendet die E-Mail erneut, wenn ich die Seite erneut aktualisiere. – Elle

Antwort

-1

Ich habe versucht, meinen PHP zu beheben. Für den Moment funktioniert das. Ich habe auf eine andere Seite umleiten nach einreichen:

Code bearbeitet:

if(count($_POST) == 0) { 
 

 
\t \t \t // L'INDIRIZZO DEL DESTINATARIO DELLA MAIL 
 
\t \t \t $to = "[email protected]"; 
 

 
\t \t \t // IL SOGGETTO DELLA MAIL 
 
\t \t \t $subject = "Segnalazione da " . $_POST["nome"]; 
 

 
\t \t \t // COSTRUIAMO IL CORPO DEL MESSAGGIO 
 
\t \t \t $body = "Contenuto della segnalazione:\n\n"; 
 

 
\t \t \t $body .= "Mail: " . trim(stripslashes($_POST["mail"])) . "\n"; 
 

 
\t \t \t $body .= "Nome: " . trim(stripslashes($_POST["nome"])) . "\n"; 
 

 
\t \t \t $body .= "Età: " . trim(stripslashes($_POST["eta"])) . "\n"; 
 

 
    \t \t \t $val_select = trim(stripslashes($_POST["region"])); 
 
\t \t \t if ($val_select == "region1") { 
 
\t \t \t \t $val_select = "Udine Centro"; 
 
\t \t \t } else if ($val_select == "region2") { 
 
\t \t \t \t $val_select = "Rizzi/S. Domenico/Cormor/S. Rocco"; 
 
\t \t \t } else if ($val_select == "region3") { 
 
\t \t \t \t $val_select = "Laipacco/San Gottardo"; 
 
\t \t \t } else if ($val_select == "region4") { 
 
\t \t \t \t $val_select = "Udine sud"; 
 
\t \t \t } else if ($val_select == "region5") { 
 
\t \t \t \t $val_select = "Cussignacco"; 
 
\t \t \t } else if ($val_select == "region6") { 
 
\t \t \t \t $val_select = "S. Paolo/S. Osvaldo"; 
 
\t \t \t } else if ($val_select == "region7") { 
 
\t \t \t \t $val_select = "Chiavris/Paderno"; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t $body .= "Area: " . $val_select . "\n"; 
 

 
\t \t \t $body .= "Messaggio: " . trim(stripslashes($_POST["commento"])) . "\n"; 
 

 
\t \t \t // INTESTAZIONI SUPPLEMENTARI 
 
\t \t \t $header_from = "From: " . trim(stripslashes($_POST["nome"])) . " " . trim(stripslashes($_POST["mail"])) . " "; 
 

 
\t \t \t // INVIO DELLA MAIL 
 
\t \t \t if($_POST["nome"] != "" && $_POST["commento"] != "" && $_POST["eta"] != "" && $_POST["region"] != ""){ 
 
\t \t \t \t //echo "ciao!"; 
 
\t \t \t \t //mail($to, $subject, $body, $header_from); 
 
\t \t \t \t mail($to, $subject, $body, 'Mail dal sito Prima Udine'); 
 
\t \t \t \t //header("Location:https://www.google.com/"); 
 
\t \t \t \t //header("Location: redirect.php"); 
 
\t \t \t } 
 
\t \t } else (count($_POST) > 1) { 
 
      
 
\t   /* ... process form here ... */ 
 
\t   
 
\t   /* prevent re-posting prompt 
 
\t    by redirecting to same url with a 303 status */ 
 
\t   header("Location: redirect.php"); 
 
\t   exit; 
 
     }

der neue Code ist:

<?php 
 

 
\t \t if(count($_POST) == 0) { 
 

 
    /* my old code */ 
 
\t \t 
 
    } 
 
\t \t } else (count($_POST) > 1) { 
 
      
 
\t   /* ... process form here ... */ 
 
\t   
 
\t   /* prevent re-posting prompt 
 
\t    by redirecting to same url with a 303 status */ 
 
\t   header("Location: redirect.php"); 
 
\t   exit; 
 
     } 
 

 
\t \t ?> 
 
    
 

Verwandte Themen