title: Sending Data From A PHP Script Back To A JQuery AJAX Request
body: <p>I am creating a web application and have the following problem. </p>
  
  <p>In my application the user is working within a single page, they draw on a canvas. There is a single button called "Save". This takes the users ID and whatever they have created in the canvas and sends it to a database. This all works fine. The save function resemebles this:</p>
  
  <pre><code>$.ajax({
      url: "/database/write.php",
      type: "POST",     
      data: { 
          docName: name,
          docData: document_Data,
          docMode: "new"
      }, 
      success: function(html) {
          alert("Successfully Saved NEW document");
          set_Mode();
      },          
  });
  </code></pre>
  
  <p>The above AJAX request does send the three values to the PHP script which then successfully creates a new document in the database, what i need to do now is change the application mode from saving a new document to editing the previously saved document. This means that when a user saves again, they will write to the same row, overwriting the previous version of the document.</p>
  
  <p>When i send the data to the <code>write.php</code> it does write the data to the DB and the queries the database for that inserted document and retrieves its unique document ID. with that ID the application can the select that document and overwrite it. To retrieve the document ID from the query, i use the following code in <code>write.php</code></p>
  
  <p><strong>write.php</strong></p>
  
  <pre><code>$_SESSION['DOCUMENT_ID'] = $DOCUMENT_ID;
  </code></pre>
  
  <p>This $DOCUMENT_ID is the document ID retrieved from the <code>SELECT</code> query. The script then finishes and transfers control back to the main application page.</p>
  
  <p>Back on the application page i try to retreive the value but it doesnt seem to work. I can retrieve <code>$_SESSION</code> values that were set when the user first accesses the application (<code>id</code>) but now values set by the <code>write.php</code> (<code>DOCUMENT_ID</code>) page. For example, below shows the function called after the AJAX request has been successful:</p>
  
  <pre><code>function set_Mode()
  {
      var PHPvar_01 = &lt;?php echo($_SESSION['id']); ?&gt;;
      alert(PHPvar_01); //WORKS FINE
  
      var PHPvar_02 = &lt;?php echo($_SESSION['DOCUMENT_ID']); ?&gt;;
      alert(PHPvar_02); //DOES NOT WORK.          
  };
  </code></pre>
  
  <p>How should i go about sending data retrieved from the PHP query script to the application, because <code>$_SESSION</code> does not seem to work here.</p>
  
  <p>Thanks for any feedback.</p>