0

New to this, how to save data into a specific div when clicked?

<form id="form">
 <div id="add">
  <input type="number" class="pts">
   <textarea class="rmks"></textarea>
 </div>
<div id="sub">
 <input type="number" class="pts">
   <textarea class="rmks"></textarea>
</div>
<button type="button" class="save">SAVE</button>
</form>

I don't know what's the correct way to do this. cant save data in database under <div id="sub">

<script>
$('.save').on('click',function(){
 var pts = $('.pts').val();
 var rmks = $('.rmks').val();
  $.ajax({
   url:"insert.php",
   method:"POST",
   data:{pts:pts,rmks:rmks},
   success:function(){
     swal({
      title:"Success!",
      timer:3000,
      icon:"success",
     })
    }
   })
  })
</script>
Newbee
  • 23
  • 4
  • Your question is unclear... Pls elaborate.. what do you want to achieve? what is not working? any specific error is coming? – Anant Kumar Singh May 17 '22 at 04:54
  • @AnantKumarSingh i cant save data in database under `
    `.
    – Newbee May 17 '22 at 05:23
  • is the ajax request fails? at a glance the js code looks alright, maybe your problem is in `insert.php`. – Bagus Tesa May 17 '22 at 05:32
  • nope, Op wants to send multiple values which have the same class so eventually, only one value will go. – Anant Kumar Singh May 17 '22 at 05:43
  • @Newbee you need to read HTML/jquery properly first and find out how to send form data using jQuery.(with array format of data) – Anant Kumar Singh May 17 '22 at 05:44
  • So you want to save *from* a specific div? ie ignore `.add` div and only use `.sub`? This line `$('.pts').val();` will get the first .pts in the document, so you need to add `.sub` to the selector, eg `$('.sub .pts').val();` or equivalent `$('.sub;).find('.pts').val();`. Similarly for `$(".sub .rmks")` – freedomn-m May 17 '22 at 08:43

1 Answers1

0

Maybe you could read form data instead of getting inner html value. Read here:

How can I get form data with JavaScript/jQuery?

Also, can you paste insert.php code ? It will be easier to help you as well