2017-08-13 11 views
0

CodeIgniter3フレームワークでコードを書きました。私は入力フィールドの直後にフォームの検証をAjaxで示すコードを書いた。しかし、私は機能「画像をアップロード」を追加し、この理由から、私のAJAXのファイルを変更:にAjaxでフォームをFormDataに変更した後で、codeigniterフォームの検証が機能しませんでした。

$(document).ready(function() { 
$("#continueregistrationform").unbind('submit').bind('submit', function() { 
    var form = $(this); 


$.ajax({ 
    url: form.attr('action'), 
    type: form.attr('method'), 
    data: form.serialize(), 
    async : false, 
    cache : false, 
    processData : false, 
    dataType: 'json', 
    success:function(response) { 
    console.log(response);    
     if(response.success) {     
      $("#messages").html(response.messages); 
      $("#continueregistrationform")[0].reset(); 
      $(".text-danger").remove(); 
      $(".form-group").removeClass('has-error').removeClass('has-success'); 
      location.href = "http://localhost/edu-center/"; 

     } 
     else { 
      $("#messages").html(response.messages); 
      $.each(response.messages, function(index, value) { 
       var element = $("#"+index); 
       $(element).parent('div').find('.text-danger').remove(); 
       $(element).after(value); 

      }); 
     } 
    } // /success 
}); // /ajax 

return false; 
}); 
}); 

:代わりに、入力フィールドの下に前のようなエラーを示すの

$(document).ready(function() { 
$("#continueregistrationform").unbind('submit').bind('submit', function() { 
    var formData = new FormData($('#continueregistrationform')[0]); 
$.ajax({ 
    url: formData.attr('action'), 
    type: formData.attr('method'), 
    data: formData, 
    async : false, 
    cache : false, 
    contentType: false, 
    processData : false, 
    dataType: 'json', 
    success:function(response) { 
    console.log(response);    
     if(response.success) {     
      location.href = "http://localhost/edu-center/"; 
     } 
     else { 
      $("#messages").html(response.messages); 
      $.each(response.messages, function(index, value) { 
       var element = $("#"+index); 
       $(element).parent('div').find('.text-danger').remove(); 
       $(element).after(value); 

      }); 
     } 
    } // /success 
}); // /ajax 

return false; 
}); 
}); 

さて、それは示していそれらを新しい空白の白いページに入れます。 enter image description hereどこに問題がありますか?前もって感謝します!

答えて

0

私は個人的にformData.attrを使用してアクションとメソッドを設定しません。 formData.attr( 'action')とformData.attr( 'method')の値が期待どおりであることを確認することができます。

私自身の個人的な経験から、あなたがやっている正確に何をして、これは私がAjaxのリクエストに画像やポスト変数を追加する方法です:あなたはこのコードをしてください説明できる

// Add file 
var formData = new FormData(); 
var fileInput = $('#your_image_upload_field')[0]; 
var file = fileInput.files[0]; 
formData.append('userfile[]', file); // MUST HAVE [] OR PHP HAS NO $_FILES !!! 
// Add post vars 
formData.append('a', a); 
formData.append('b', b); 
formData.append('c', c); 
// Do post 
$.ajax({ //... 
+0

? –

+0

それはうまくいきませんでした:( –

+0

あなたはAJAXリクエストをしていないようですが、あなたのフォームはそのアクションに投稿しているだけです。AJAXリクエストのURLをハードコードすればどうなりますか? –

関連する問題