PHP submit form without refreshing
<p>When the user submits the form, the result should be displayed without page refreshing. The PHP script is also in the same HTML page.</p>

<p>What is wrong withe <code>$.post</code> jQuery?</p>

<p><div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!--
 Submit form without refreshing
--&gt;

&lt;html&gt;

&lt;head&gt;
  &lt;title&gt;My first PHP page&lt;/title&gt;
  &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" language="javascript"&gt;
    $(document).ready(function() {
      $("#btn").click(function(event) {
        var myname = $("#name").val();
        var myage = $("#age").val();
        $.post(
          "23.php", $("#testform").serialize()
        );
      });
    });
  &lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;form action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="post" id="testform"&gt;
    &lt;!-- $_SERVER['PHP_SELF'] array --&gt;
    Name:
    &lt;input type="text" name="name" id="name" /&gt;Age:
    &lt;input type="text" name="age" id="age" /&gt;
    &lt;input type="submit" name="submit" id="btn" /&gt;
  &lt;/form&gt;
&lt;/body&gt;

&lt;/html&gt;</code></pre>
</div>
</div>
</p>

<pre><code>&lt;?php
    if ( isset($_POST['submit']) ) { // was the form submitted?
        echo "Welcome ". $_POST["name"] . "&lt;br&gt;";
        echo "You are ". $_POST["age"] . "years old&lt;br&gt;";
    }
?&gt;
</code></pre>