2016-01-27 43 views
5

我試圖用bootstrap和bootstrap驗證器製作html頁面。帶模式,驗證器和電子郵件的Bootstrap表格

我想要做的事情: 當用戶點擊按鈕時,模態會以表格的形式出現。 驗證通過後,表單發送了一個包含字段值的電子郵件。 當郵件被正確發送,其他模式出現與一些信息

我的問題: 我的腳本與引導驗證器不起作用。當某個字段出錯時,每次出現錯誤時都會發送該表單。 如果我完成所有的領域,頁面重新啓動,並沒有工作。

拜託,你能幫我找到我的錯誤嗎?

我的html:

<html lang="fr"> 
<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 

<title></title> 

<!-- Bootstrap --> 
<link href="css/bootstrap.css" rel="stylesheet"> 
<style type="text/css"> 

</style> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> 
<script type="text/javascript" src="js/bootstrapValidator.js"></script> 

<script language="JavaScript"> 
<!--vérif formulaire--> 

     function verif() { 
      $('#contact') 
      .bootstrapValidator({ 
       live: 'disabled', 
       message: 'Cette valeur est invalide', 
       feedbackIcons: { 
        valid: 'glyphicon glyphicon-ok', 
        invalid: 'glyphicon glyphicon-remove', 
        validating: 'glyphicon glyphicon-refresh' 
       }, 
       fields: { 
        e1: { 
         validators: { 
          notEmpty: { 
           message: 'Votre nom est obligatoire' 
          } 
         } 
        }, 
        e2: { 
         validators: { 
          notEmpty: { 
           message: 'Votre prenom est obligatoire' 
          } 
         } 
        }, 
        e3: { 
         validators: { 
          notEmpty: { 
           message: 'Votre adresse mail est obligatoire' 
          }, 
          /*emailAddress: { 
          message: 'Le format de votre adresse mail n est pas valide' 
          }*/ 
         } 
        }, 
        e4: { 
         validators: { 
          notEmpty: { 
           message: 'Votre numero de telephone est obligatoire' 
          } 
         } 
        }, 
        /*homePhone: { 
         validators: { 
          digits: { 
           message: 'The home phone number is not valid' 
          } 
         } 
        },*/ 
       } 
      }) 
      .on('error.form.bv', function(e) { 
       console.log('error.form.bv'); 

       var $form = $(e.target); 
       console.log($form.data('bootstrapValidator').getInvalidFields()); 

      }) 
      .on('success.form.bv', function(e) { 
       console.log('success.form.bv'); 
       envoimail(); 
      }) 

     } 

     function envoimail() { 
      alert("lancement mail ok"); 
      $.ajax({ 
       type: "POST", 
       url: "process.php", 
       data: $('form.contact').serialize(), 
       success: function(msg){ 
        $("#thanks").html(msg); 
        alert("thanks ok"); 
        $("#myModal").modal('hide'); 
        alert("hide ok"); 
        $("#synthese").modal('show'); 
        alert("show ok"); 
       }, 
       error: function(){ 
        alert("Echec de l envoi du formulaire"); 
       }, 
      }); 
     }; 

    </script> 

</head> 
<body> 
    <div> 
     <button type="button" class="btn btn-primary btn-lg center-block" data-toggle="modal" data-target="#myModal"> 
      Lancer le calcul 
     </button> 
    </div> 

    <div id="thanks"> 
     <p>test</p> 
    </div> 

    <br> 

<!-- Modal 1 --> 
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> 
     <div class="modal-dialog modal-lg" role="document"> 
      <div class="modal-content"> 

       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title" id="myModalLabel">Quelques informations sur vous :</h4> 
       </div> 

       <div class="modal-body"> 
        <form id="contact" class="form-horizontal contact" name="contact"> 
         <div class="form-group"> 
          <label class="col-lg-4 control-label">Nom</label> 
          <div class="col-lg-6"> 
           <input class="form-control" id="e1" name="e1" type="text" style="text-align:left" /> 
          </div> 
         </div> 

         <div class="form-group"> 
          <label class="col-lg-4 control-label">Prénom</label> 
          <div class="col-lg-6"> 
           <input class="form-control" id="e2" name="e2" type="text" style="text-align:left" /> 
          </div> 
         </div> 

         <div class="form-group"> 
          <label class="col-lg-4 control-label">Fonction</label> 
          <div class="col-lg-6"> 
           <input class="form-control" id="e5" name="e5" type="text" style="text-align:left"/> 
          </div> 
         </div> 

         <div class="form-group"> 
          <label class="col-lg-4 control-label">Email</label> 
          <div class="col-lg-6"> 
           <input class="form-control" id="e3" name="e3" type="text" style="text-align:left" /> 
          </div> 
         </div> 

         <div class="form-group"> 
          <label class="col-lg-4 control-label">Téléphone</label> 
          <div class="col-lg-6"> 
           <input class="form-control" id="e4" name="e4" type="text" style="text-align:left" /> 
          </div> 
         </div> 

         <div class="form-group"> 
          <label class="col-lg-4 control-label">Message</label> 
          <div class="col-lg-6"> 
           <input class="form-control" id="e6" name="e6" type="text" style="text-align:left" /> 
          </div> 
         </div> 

         <div class="form-group"> 
          <div class="col-lg-12"> 
           <button type="submit" id="resultat" class="btn btn-primary btn-lg center-block" onclick="verif()" >Afficher le résultat</button> 
          </div> 
         </div> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 

<!-- Modal 2 -->  
    <div class="modal fade" id="synthese" tabindex="-1" role="dialog" aria-labelledby="myModalLabel""> 
     <div class="modal-dialog modal-lg" role="document"> 
      <div class="modal-content"> 

       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
        <h4 class="modal-title" id="myModalLabel">Synthèse :</h4> 
       </div> 

       <div class="modal-body"> 

        <div class="modal-body"> 
         <button type="button" class="btn right" data-dismiss="modal" style="float:right;">Relancer</button> 
        </div> 

        <br> 

       </div> 
      </div> 
     </div> 
    </div> 
    <script src="js/bootstrap.min.js"></script> 
</body> 

我process.php:

<?php 
$myemail = '[email protected]'; 
if (isset($_POST['e1'])) { 
$e1nom = strip_tags($_POST['e1']); 
$e2prenom = strip_tags($_POST['e2']); 
$e3mail = strip_tags($_POST['e3']); 
$e4tel = strip_tags($_POST['e4']); 
$e5fonction = strip_tags($_POST['e5']); 
$e6message = strip_tags($_POST['e6']); 
echo "<span class=\"alert alert-success\" >Your message has been received. Thanks! Here is what you submitted:</span><br><br>"; 
echo "<stong>Nom : </strong> ".$e1nom."<br>"; 
echo "<stong>Prenom: </strong> ".$e2prenom."<br>"; 
echo "<stong>Mail : </strong> ".$e3mail."<br>"; 
echo "<stong>Tel : </strong> ".$e4tel."<br>"; 
echo "<stong>Fonction : </strong> ".$e5fonction."<br>"; 
echo "<stong>Message : </strong> ".$message."<br>"; 

$to = $myemail; 
$email_subject = "Lancement du simulateur de prix PPE"; 
$email_body = "Lancement d une nouvelle simulation. \n\n". 
" Detail de la simulation :\n\n". 
" Nom : $e1nom \n". 
" Prenom : $e2prenom \n". 
" Mail : $e3mail \n". 
" Tel : $e4tel \n". 
" Fonction : $e5fonction \n". 
" Message :\n $e6message"; 
$headers = "From: [email protected]\n"; 
$headers .= "Reply-To: $email"; 
mail($to,$email_subject,$email_body,$headers); 
}?> 
+0

讓你的字段名字符串,如E1 --->「E1」 – Monty

+0

對不起,我不明白我需要做什麼: -/ – Gurol

+0

試試這個並編譯你的代碼。你面臨什麼錯誤? – Monty

回答

2

當談到通過JavaScript你應該總是驗證服務器端以及反正形式(因爲你可以」不要相信訪問者發給你的任何東西),除此之外,當使用提交按鈕時,沒有什麼能夠阻止表單提交。

因此,無論是將表單按鈕類型更改爲'按鈕'而不是'submit',或者在驗證開始時添加一些內容以阻止表單運行默認操作。沿着線的東西 -

function verif(){$('#contact').preventDefault();$('#contact')....

甚至更​​好,同時使用。這樣,表格將不會提交,直到檢查結果與表格無關。

+0

感謝您的回答,但我是一名初學者,我不知道我需要如何做到這一點...你能幫我多一點嗎? – Gurol

+0

然後,找到設置爲'''type ='submit''''的表單上的按鈕並將其更改爲'''type ='button'''' - 這樣可以防止表單被提交函數verify(){$('#contact')。函數verif()'''修改它的開始。 '''函數verif(){$('#contact')。的preventDefault(); $( '#' 接觸)。bootstrapvalidator(......等等等等 ''' –

+0

喜歡這個? '函數VERIF(){ \t \t \t $( '#' 接觸) \t \t \t \t .preventDefault({ \t \t \t \t}) \t \t \t \t .bootstrapValidator({ \t \t \t \t \t活:「DISABL ED」, \t \t \t \t \t信息: 'CETTE valeur EST invalide', \t \t \t \t \t feedbackIcons:{ \t \t \t \t \t \t有效的: 'glyphicon glyphicon-OK', \t \t \t \t \t \t無效:'glyphicon glyphicon-remove', \t \t \t \t \t \t validat ...' 我有一個錯誤: '遺漏的類型錯誤:$(...)的preventDefault不是function' – Gurol

1

評論這一行:直播: '已禁用',

建議:

1.) // Validate the form manually 
     $('#resultat').click(function() { 
      $('#contact').bootstrapValidator('validate'); 
     }); 

2.) write your code in document.ready function. 

修改:

.on('error.field.bv', function(e, data) { 
    //console.log('error.field.bv -->', data.element); 
}) 
.on('success.field.bv', function(e, data) { 
    //console.log('success.field.bv -->', data.element); 
    // envoimail(); 
}) 

.on('success.form.bv', function(e) { 
      // Prevent form submission 
      e.preventDefault(); 

      // Get the form instance 
      var $form = $(e.target); 

      // Get the BootstrapValidator instance 
      var bv = $form.data('bootstrapValidator'); 

      // Use Ajax to submit form data 
      $.post('process.php', $form.serialize(), function(result) { 
       console.log(result); 
      }, 'json'); 
     }); 
+0

我試過,但我需要把我的「envoimail()」功能? – Gurol

+0

檢查我的修改答案。 @Gurol – Monty

+0

無法正常工作。當我點擊按鈕時,envoimail()函數將被啓動多次,因爲我必須輸入 – Gurol

0

變化從「提交」按鈕類型爲「按鈕」,然後在JavaScript中,你可以添加這個,如果條件

if($('#contact').validate().form()){ 
    envoimail(); 
}); 
+0

'Uncaught TypeError:$(...)。validate不是一個函數'。缺了點什麼 ? – Gurol

相關問題