0

In javascript this works easily, wordpress requires jquery ajax, and I'm not the most familiar with jquery. Is the following example possible with ajax jquery?

How to send multiple variables so that the "var notei++" increase by one number, send as:

note1 = "My note 1",
note2 = "My note 2",
note3 = "My note 3",
note4 = "My note 4",

and more.....

var i = "";
var note = "";
var divsname = document.getElementsByClassName("notes");
for (i = 1, i++) {
  divsname.forEach(function(div) {
    note += note i++ = div.innerHTML;
  });
  jQuery(document).ready(function($) {
    var data = {
      'action': 'my_actione',
      'user_id': '1',
      note
    };
    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
    jQuery.post(ajaxurl, data, function(response) {
      alert('Got this from the server: ' + response);
    });
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notes">My note 1</div>
<div class="notes">My note 2</div>
<div class="notes">My note 3</div>
<div class="notes">My note 4</div>
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
maja
  • 177
  • 1
  • 13

1 Answers1

1

You can send your div data using JSON Array with index number and value is the text inside your div and then pass same to your ajax.

Demo Code :

var divsname = $(".notes");
notes = []
divsname.each(function(i) {
  //push in json array
  notes.push({
    [`note${i + 1}`]: $(this).text()
  })
});
var data = {
  'action': 'my_actione',
  'user_id': '1',
  'note': notes //pass same
};

console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="notes">My note 1</div>
<div class="notes">My note 2</div>
<div class="notes">My note 3</div>
<div class="notes">My note 4</div>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • So it is not possible to somehow set jquery ajax key value as variable. Ok can you write everything except jqery ajax in javascript because I'm not familiar with the jquery script (as in my example above) – maja Mar 07 '21 at 11:49
  • Hi, you can just pass `data` inside your ajax and i have already added that inside `notes` inside data here `var data = {.. 'note': notes //pass same };..` and you can see output of `data` when you run above code snippet . – Swati Mar 07 '21 at 11:54
  • Swati, can the received.php page read $ _POST ['note1'], $ _POST ['note2'] ..., this way – maja Mar 07 '21 at 12:03
  • so you don't need them in json array ? you need to pass them separately ? with above code you can simply use for loop and get all [datas](https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – Swati Mar 07 '21 at 12:10
  • That I need to switch them separately so they are received.php can read each separately..., how? – maja Mar 07 '21 at 12:14
  • can you show current code of php which you are using currently to receive data ? – Swati Mar 07 '21 at 12:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229616/discussion-between-swati-and-maja). – Swati Mar 07 '21 at 12:31