2017-05-16 238 views
1

發送電子郵件,我有我的形式在HTML:使用JavaScript,HTML和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> 

然後,我在PHP連接我的形式在HTML與我的電子郵件發件人代碼:

<?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"); 


      } 
     ?> 

但是這種方法不工作。所以我想改變方法。我想將我的HTML表單與使用Ajax的JavaScript代碼連接來發送電子郵件。 我該怎麼做?如果您需要更多信息,請問我,謝謝。 ps。我是新手,但我可以編碼的東西;)

更新:我試圖解決PHP的問題,檢查我的答案。

+3

什麼是「唐不行k「是什麼意思?你有錯誤嗎? –

+0

你必須使用自己的代碼嗎?如果不是,那麼你可以給http://swiftmailer.org/試試.. – lU5er

+0

@ScottMarcus嗯,唯一不行的是我提交電子郵件時,我刷新頁面的表單再次提交電子郵件。我已經嘗試了所有的代碼和網絡上的解決方案....當我再次刷新頁面時,它會繼續重新發送電子郵件。 – Elle

回答

-1

我試圖修復我的php。目前來看,這是有效的。我重定向到另一個頁面提交後:

代碼編輯:

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; 
 
     }

新的代碼是:

<?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 ?>