BackEND/PHP

한번에 여러개의 데이타를 객체형태로 ajax사용해서 넘기기.

smartlittlepuppy 2023. 7. 9. 18:58
반응형

주어진 필드들을 한 번에 처리하기 위해서는 객체를 사용하여 데이터를 구조화.

<input type="text" id="price">
<input type="text" id="qty">
<input type="text" id="title">
<input type="text" id="fullname">
<input type="text" id="memo">
<input type="text" id="file">
<input type="text" id="section">
<input type="text" id="category">
<!-- 필요한 만큼 필드를 추가할 수 있습니다 -->

 

$("form").submit(function(e) {
  e.preventDefault();

  var data = {
    price: $("#price").val(),
    qty: $("#qty").val(),
    title: $("#title").val(),
    fullname: $("#fullname").val(),
    memo: $("#memo").val(),
    file: $("#file").val(),
    section: $("#section").val(),
    category: $("#category").val()
  };

  $.ajax({
    url: "includes/login.php",
    method: "POST",
    data: data,
    success: function(response) {
      console.log(response);
    },
    error: function(xhr, status, error) {
      console.error(xhr.responseText);
    }
  });
});

반응형