title: How to get variable value from PHP to Java Script via AJAX?
body: <p>I have created a chat website. I send the message with AJAX to PHP and the MySql Database. The messages are fetched using AJAX which runs per second. But this lead to fetch of all the messages (from starting to end). I came with an solution that I will pass the last message ID to the AJAX/JAVA SCRIPT and then fetch only the messages which are more than that.</p>
  <p>Here is the Java Script / AJAX</p>
  <pre><code>function fetchdata(){
  var cuser = //id of the current user 
  var ouser = //id of the other user
   $.ajax({
    url: &quot;messagesprocess.php&quot;,
    type: &quot;POST&quot;,
    data : {cuser:cuser, ouser:ouser},
    success: function(read){
      $(&quot;#readarea&quot;).html(read);
    }
   });
  }
  </code></pre>
  <p>Here is the PHP code to get messages:</p>
  <pre><code>$sql = &quot;SELECT id, fromid,message,toid FROM messages WHERE (fromid={$_POST['cuser']} AND toid={$_POST['ouser']}) OR (fromid={$_POST['ouser']} AND toid={$_POST['cuser']})&quot;;
  $result = mysqli_query($conn, $sql) or (&quot;Query Failed&quot;);
  
  while($row=mysqli_fetch_assoc($result)){
    if($row[&quot;fromid&quot;]==$_POST['cuser']){
      echo &quot;&lt;div class='cuser'&gt;&quot;.$row[&quot;message&quot;].&quot;&lt;/div&gt;&quot;;
    }else{
      echo &quot;&lt;div class='ouser'&gt;&quot;.$row[&quot;message&quot;].&quot;&lt;/div&gt;&quot;;
    }
  }
  </code></pre>
  <p>Here I want to get the ID (message) in the Java Script function back from the PHP and use it as a variable for fetching the messages which will be more than it.</p>