title: How to update the SESSION value on same page after AJAX call on post JSON value without refreshing page
body: <h1>How to update the SESSION value on same page after AJAX call on post JSON value without refreshing page.</h1>
  <p>I'm stuck here. Any help solving the problem is highly appreciated. I what to run particular class in PHP based on the action button clicked. I.e When the user clicks the add button it initiates the AJAX call and sends the 'ADD' ID else if he/she clicks the update button it sends the 'UPDATE ID.  I want to perform this without updating the page. <br> <br>
  I have achieved getting the data, but the only issue is it <strong>updates only when I refresh the page</strong> <br><br></p>
  <p>How do I update the SESSION in PHP once the AJAX sends the data. I'm a newbie, please take easy on me :) Thanks u</p>
  <h3>Index.php</h3>
  <pre><code>&lt;?php
  session_start();
  echo &quot;SESSION: &quot;.$_SESSION['pageType'];
  ?&gt;
  
  &lt;!DOCTYPE&gt;
  &lt;html lang=&quot;en-OM&quot;&gt;
  
  &lt;div &gt;AJAX: &lt;/div&gt;&lt;div id=&quot;msg&quot; style=&quot;display:inline&quot;&gt;&lt;/div&gt;&lt;/div&gt;
  &lt;a id=&quot;btnAdd&quot; value=&quot;add&quot; class=&quot;btnType&quot; href=&quot;#&quot; role=&quot;button&quot;&gt;ADD&lt;/a&gt;&lt;br&gt;
  &lt;a id=&quot;btnUpdate&quot; value=&quot;update&quot; class=&quot;btnType&quot; href=&quot;#&quot; role=&quot;button&quot;&gt;UPDATE&lt;/a&gt;&lt;br&gt;
  &lt;a id=&quot;btnDelete&quot; value=&quot;delete&quot; class=&quot;btnType&quot; href=&quot;#&quot; role=&quot;button&quot;&gt;DELETE&lt;/a&gt;
  
  &lt;/html&gt;
  </code></pre>
  <h3>Jquery</h3>
  <pre><code>   &lt;script&gt;
      $(document).ready(function () {
          $('.btnType').click(function (ev) {
              ev.preventDefault();
              ev.stopImmediatePropagation();
              let pageID;
              pageID = $(this).attr('value');
              $.ajax({
                  type: 'POST',
                  url: 'postData.php',
                  dataType: 'json',
                  data: {
                      pageType: pageID
                  },
                  beforeSend: function (data) {
                  },
                  success: function (data) {
                      $('#msg').text(data);
                      console.log(data);
                      return data;
                  },
                  error: function (jqXHR, textStatus, errorThrown) {
                      console.log('&lt;p&gt;status code: ' + jqXHR.status +
                          '&lt;/p&gt;&lt;p&gt;errorThrown: ' + errorThrown +
                          '&lt;/p&gt;&lt;p&gt;jqXHR.responseText:&lt;/p&gt;&lt;div&gt;' + jqXHR.responseText +
                          '&lt;/div&gt;');
                      console.log('jqXHR:');
                      console.log(jqXHR);
                      console.log('textStatus:');
                      console.log(textStatus);
                      console.log('errorThrown:');
                      console.log(errorThrown);
                  }
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  <h3>postData.php</h3>
  <pre><code>session_start();
  if (!empty($_POST['pageType'])) {
      echo json_encode($_POST['pageType']);
      $_SESSION['pageType']=$_POST['pageType'];
      exit;
  }
  </code></pre>