title: Send data with POST in jQuery to PHP in the same file
body: <p>I have mail.php file which contains two columns. One consists of a list of mails titles that I get with PHP from database (id of elements is set to mail id as it is in database). The other column should show message of the selected mail.</p>
  
  <p>I have made a JS script which sets selected mail to active and gets its id attribute. Now I wan to send this id to PHP so that I can get message of the selected mail.</p>
  
  <p>How can I use POST method to send id of selected item and get it with PHP in the same file?
  Or if someone could explain me some better way to do it?</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;...&lt;/head&gt;
  &lt;body&gt;
  &lt;div class="container"&gt;
    &lt;div class="row text-center"&gt;
        &lt;div class="col-md-4 col-sm-2"&gt;
          &lt;div class="list-group"&gt;
          &lt;?php get emails and set element id=Mail_ID ?&gt;
          &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="col-md-8 col-sm-10"&gt;
        &lt;div class="mail-text"&gt;
          &lt;?php get id form JS script and show message ?&gt;
         &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
  
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;script&gt;
    $('.list-group a').on('click', function (event) {
      event.preventDefault();
      $('.active').removeClass('active');
      $(event.target).closest('a').addClass('active');
      var active = $(event.target).closest('a').attr('id');
  
    });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I didn't even have to use POST method to get the id of the email.
  I managed to solve my problem like this:
  1) First I saved mail text and id in the $messages array such that id is the key and message is the value. I did this in the div with class list-group when i fetch all data from database.
  2) then in the script section I just changed the text in div with mail-text class like this</p>
  
  <pre><code>&lt;script&gt;
    $('.list-group a').on('click', function (event) {
      event.preventDefault();
      $('.active').removeClass('active');
      $(event.target).closest('a').addClass('active');
      var Mail_id = $(event.target).closest('a').attr('id');
      var mail_text=&lt;?php echo json_encode($messages); ?&gt;;
      var joined = mail_text[Mail_id];
      $('.message').text(joined);
    });
  &lt;/script&gt;
  </code></pre>