title: ajax post javascript variable to php in same page
body: <p>i write sample code to ajax post <strong>javaxcript varible to php</strong> in 2 page, test.php and validate.php.</p>
  
  <p><strong>test.php :</strong>  </p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;script type="text/javascript" src="jquery.min.js" &gt;&lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form&gt;
  &lt;input type="text" id="name" placeholder="enter Your name...." /&gt;&lt;br/&gt;
  &lt;input type="text" id="age" placeholder="enter Your age...." /&gt;&lt;br/&gt;
  &lt;input type="button" value="submit" onclick="post();"&gt;
  &lt;/form&gt;
  &lt;div id="result" &gt;&lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
  function post()
   {
  var name=$('#name').val();
  var age=$('#age').val();
  $.post('validate.php',{postname:name,postage:age},
  function(data)
  {
  if (data=="1")
      {
  $('#result').html('you are over 18 !');
  }
  if (data=="0")
  {
  $('#result').html('you are under 18 !');
  }
  });
  }
  &lt;/script&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>validate.php</strong></p>
  
  <pre><code>&lt;?php
  $name=$_POST['postname'];
  $age=$_POST['postage'];
  
  if ($age&gt;=18)
  {
  echo "1";
  }
  else
  {
  echo "0";
  }
  ?&gt;
  </code></pre>
  
  <p><strong>how can i write/change above code in same page ?? and validate.php insert in test.php ??</strong></p>
  
  <p>added after first answer :
  "but i dont have/use any button or  in my page. 
  and where can i insert validate.php code in test.php. please help by insert complete correct code."</p>