2017-03-25 99 views
1

我最近開始學習AJAX的形式提交,但似乎我仍然失去了一些東西必不可少的,因爲喜歡它的假設所提交的數據也不會去的PHP文件。我不明白爲什麼它不起作用。jQuery驗證插件+ AJAX + PHP不工作

HTML:

<div class="col-md-6"> 
    <form class="form" action="../wp-content/themes/tempo/cfgs/js/scrollmagic/contact.php"> 
Eesnimi: 
<input name="eesnimi" id="eesnimi" class="required" type="text"> 
Perekonnanimi: 
<input name="pernimi" id="pernimi" class="required" type="text"> 
E-Post: 
<input name="email" id="email" class="required" type="email"> 
Telefoni number: 
<input name="nr" id="nr" class="required" type="text"> 
Teema: 
<input name="teema" id="teema" class="required" type="text"> 
Sõnumi sisu: 
    <textarea name="sisu" id="sisu" class="required"></textarea> 
<div></div> 
    <input type="submit" id="sub" value="Edasta"> 
</form> 
</div> 

JQUERY/AJAX

$('#sub').click(function(){ 
    $('.form').submit(); 
    var eesnimi = $('#eesnimi').val(); 
    var pernimi = $('#pernimi').val(); 
    var email = $('#email').val(); 
    var nr = $('#nr').val(); 
    var teema = $('#teema').val(); 
    var sisu = $('#sisu').val(); 

    $('.form').validate({ 
     rules:{ 
      eesnimi: { 
      required: true, 
      nowhitespace: true, 
      lettersonly: true 
      }, 
      pernimi: { 
      required: true, 
      nowhitespace: true, 
      lettersonly: true 
      }, 
      email: { 
      required: true, 
      email: true 
      }, 
      nr: { 
      required: false, 
      integer: true 
      }, 
      teema: { 
      required: true 
      }, 
      sisu: { 
      required: true 
      } 
     }, 
     messages:{ 
      eesnimi: { 
      required: valituhiErr, 
      nowhitespace: tuhikErr, 
      lettersonly: nonumbersErr 
      }, 
      pernimi: { 
      required: valituhiErr, 
      nowhitespace: tuhikErr, 
      lettersonly: nonumbersErr 
      }, 
      email: { 
      required: valituhiErr, 
      email: emailErr 
      }, 
      nr: { 
      required: valituhiErr, 
      integer: onlynumbersErr 
      }, 
      teema: { 
      required: valituhiErr, 
      }, 
      sisu: { 
      required: valituhiErr, 
      } 
     }, 
     submitHandler: function(){ 
      $.ajax({ 
      type: "POST", 
      url: "../wp-content/themes/tempo/cfgs/js/scrollmagic/contact.php", 
      data: $('.form').serialize(), 
      success: function (o) { 
       console.log(o); 
      } 
     }); 
     } 
    }); 
    return false; 
}); 

PHP:

<?php 
print_r($_POST); 
?> 

我只是嘗試,似乎驗證工作正常,但提交功能贏得了」 T,beceause服務器將不會從我的Ajax請求收到任何東西,也不會在控制檯上顯示任何東西無論是。

+2

您發佈的數據,然後在你的PHP打印出GET查詢字符串的數據。嘗試'的print_r($ _ POST)' –

+0

是啊,那只是一個錯字,我已經嘗試過兩種方式,mybad不固定這一點。 – drpzz

回答

1

試試這個:jQuery的

var data = { 
    eesnimi = $('#eesnimi').val(); 
pernimi = $('#pernimi').val(); 
    email = $('#email').val(); 
    nr = $('#nr').val(); 
    teema = $('#teema').val(); 
    sisu = $('#sisu').val(); 
     }; 
    $.ajax({ 
      type : "post", 
      dataType : "JSON", 
      url : "register.php", 
      data : data, 
      success : function(result) 
      { 

       if (result.hasOwnProperty('error') && result.error == '1'){ 
        var html = ''; 


        $.each(result, function(key, item){ 

         if (key != 'error'){ 
          html += '<li>'+item+'</li>'; 
         } 
        }); 
        //show in html 

PHP

eesnimi = $_POST['eesnimi'] 
pernimi = ... 
    email =... 
    nr = ... 
    teema = ... 
    sisu = ... 
//connect db 
//sqlquery you want validate 
if (mysqli_num_rows($result) > 0) 
{ 
    $row = mysqli_fetch_assoc($result); 
    if ($row['eesnimi']==$eesnimi){ 
     $errors['eesnimi '] = ...; 
    } 
// like this 
if (count($errors) > 1){ 
    $errors['error'] = 1; 
    die (json_encode($errors)); 
} 
//insert into db 

} 

希望這有助於

+0

謝謝你的男人!同樣的事情也該再次爲我工作,謝謝! – drpzz

+0

很酷。如果你認爲這有幫助。投我一票 。哈哈 – Akashii