title: Saving values from php $_SESSION through $_POST in form inputs with ajax
body: <p>How to add ajax sent data in form inputs to be saved as session. I have session_start() in header.php. I'm saving the code in the database and it works. Thanks.
  I tried this:</p>
  
  <p><strong>HTML form</strong> </p>
  
  <pre><code>&lt;form action='' method='post'&gt;
    &lt;input id='code' type='text' name='code'&gt;
    &lt;a id='send_code'&gt;Send code&lt;/a&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>AJAX</strong> (I have a submit handler for this)</p>
  
  <pre><code>var code = $('#code').val();
  $.ajax({
      type: "POST",
      url: ajaxurl,
      data: {action: 'insert_code', code: code},
      success: function(msg){
          console.log(msg);
      }
  });
  </code></pre>
  
  <p><strong>PHP from functions.php</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>$code = ( isset( $_POST['code'] ) ) ? htmlspecialchars( trim( $_POST['code'] ) ) : '';
  $_SESSION['contest'] = array(
      'code'  =&gt; $code,
  );
  
  if( isset( $_SESSION['contest']) ){
      echo 'Session: ' . $_SESSION['contest']['code'];
  }else{
      echo "Session not set";
  }
  </code></pre>