connecting HTML to PHP file using ajax : <p>I have created an application using xampp (apache and mysql). I have the following HTML code:</p>

<pre><code>     &lt;!DOCTYPE html&gt;
        &lt;html&gt;
        &lt;head&gt;
        &lt;title&gt;Name&lt;/title&gt;
        &lt;/head&gt;
        &lt;body&gt;

    &lt;div id="main"&gt;
    &lt;h1&gt;Details&lt;/h1&gt;
    &lt;div id="name"&gt;
    &lt;h2&gt;Name&lt;/h2&gt;
    &lt;hr/&gt;

    &lt;Form Name ="form1" Method ="POST" ACTION = "name.php"&gt;

    &lt;label&gt;Name: &lt;/label&gt;
    &lt;input type="text" name="per_name" id="name" required="required" placeholder="please enter name"/&gt;&lt;br/&gt;&lt;br /&gt;

    &lt;label&gt;Age: &lt;/label&gt;
    &lt;input type="text" name="per_age" id="age" required="required" placeholder="please enter age"/&gt;&lt;br/&gt;&lt;br /&gt;

&lt;input type="submit" value=" Submit " name="submit"/&gt;&lt;br /&gt;

      &lt;/form&gt;
     &lt;/div&gt;    
    &lt;/div&gt;   
   &lt;/div&gt;   
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>and the following PHP code:</p>

<pre><code>&lt;?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "details";

$connection = new mysqli($servername, $username, $password, $dbname);


if ($connection-&gt;connect_error) {
die("Connection failed: " . $connection-&gt;connect_error);
}

$sql = "INSERT INTO persons (person_name, person_age)
VALUES ('".$_POST["per_name"]."','".$_POST["per_age"]."')";

if ($connection-&gt;query($sql) === TRUE) {
echo "person added";
} else {
echo "person not added";
}

$connection-&gt;close();
}
?&gt;
</code></pre>

<p>Instead of calling the php file using <code>&lt;Form Name ="form1" Method ="POST" ACTION = "name.php"&gt;</code> how would i create a simple ajax file to call the PHP file? i have tried to do this but can't seem to get anywhere, can anyone help me please? AJAX:</p>

<pre><code>$(document).ready(function(){
$("#submit").click(function(){

// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "name.php",
data: dataString,
cache: false,
success: function(result){
alert(result);

});
}
return false;
});
});
</code></pre>
