2017-10-23 1 views
0

사실 난 그냥 serialize에서 모든 입력 데이터를 얻으려고하지만 나이 & 같은 int 유형의 값을 얻을 수 있지만 이름의 가치를 얻을 수 없습니다. 다음 코드는 입니다.데이터 직렬화에서 값을 가져올 수 없습니다.

<html> 
    <body> 
     <form id ="form" method="post" class= "form"> 
      Name<input type = "text" name = "name" /><br> 
      Age<input type = "number" name = "age" /><br> 
      ID<input type = "number" name = "id" /><br> 
      <input type = "submit" name = "submit"><br/> 
     </form> 
     <p id="result"></p> 
     <script 
      src="https://code.jquery.com/jquery-3.2.1.min.js" 
      integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" 
     crossorigin="anonymous"></script> 
     <script> 
      $(document).ready(function() { 
       $("#form").submit(function() { 
        var data = $("#form").serialize(); 
        insertStudent(data); 
        return false; 
       }); 
       function insertStudent(data) { 
        $.ajax({ 
         url: 'process.php', 
         data: data, 
         type: 'POST', 
         dataType: 'json', 
         success: function (data, textStatus) { 
          $("#result").html(data); 
         }, 
         error: function() { 
          alert('Not OKay'); 
         } 
        }); 
       } 
      }); 
     </script> 
    </body> 
</html> 

process.php

print_r($_POST);// give error but if i try to get id then 
print_r($_POST["id"])// print value 
print_r($_POST["name"])// doesn't print name 
+0

당신이 XHR 요청 본문 및 헤더를 게시 할 수 있습니까? – madalinivascu

+0

코드가 잘 작동하고 있습니다 : https://jsfiddle.net/85rkbx27/. 브라우저 콘솔에서 요청을 디버그해야합니다. 요청 내용이 예상 한 내용과 일치하는지 확인하십시오. –

+0

당신은 id라는 이름의 폼 엘리먼트를 가지지 않으므로 틀린 페이지에있다. – madalinivascu

답변

0
$(document).ready(function() { 
    $("#form").submit(function (e) { 
     //e.preventDefault(); 
     var data =$(this).serialize(); 
     insertStudent(data); 
     return false; 
    }); 
    function insertStudent(data) { 
     $.ajax({ 
      url: 'process.php', 
      data: data, 
      type: 'post', 
      dataType: 'html', 
      success: function (data, textStatus) { 
       $("#result").html(data); 
       // console.log(data); 
      }, 
      error: function() { 
       alert('Not OKay'); 
      } 
    }); 
    } 
}); 
관련 문제