title: Insert data into MySQL database from Javascript calling PHP file
body: <p>I have the below code which calls a .php file to insert data into a MySQL database, but cant seem to get the .php file to run. Any suggestions?</p>
  
  <p>I am very new to script so i am trying this registration form example. The database, 'college', with its table, 'registration', seems to be fine, but no php code executes. It must be something really simple but i cant seem to work it out?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function() {
  $("#register").click(function() {
  var name = $("#name").val();
  var email = $("#email").val();
  var password = $("#password").val();
  var cpassword = $("#cpassword").val();
  if (name == '' || email == '' || password == '' || cpassword == '') {
  alert("Please fill all fields...!!!!!!");
  } else if ((password.length) &lt; 8) {
  alert("Password should atleast 8 character in length...!!!!!!");
  } else if (!(password).match(cpassword)) {
  alert("Your passwords don't match. Try again?");
  } else {
   //alert(name); //This line reads fine
   $.post("register.php",
   {
   name1: name,
   email1: email,
   password1: password
  }, 
  function(data) {
   if (data == 'You have Successfully Registered.....') {
   $("form")[0].reset();
  }
  alert(data);
  });
  }
  });
  });</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>&lt;?php
  $connection = mysql_connect("localhost", "root", "Winchester12"); // Establishing connection with server..
  $db = mysql_select_db("college", $connection); // Selecting Database.
  $name=$_POST['name1']; // Fetching Values from URL.
  $email=$_POST['email1'];
  echo $email;
  $password= sha1($_POST['password1']); // Password Encryption, If you like you can also leave sha1.
  // Check if e-mail address syntax is valid or not
  $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Sanitizing email(Remove unexpected symbol like &lt;,&gt;,?,#,!, etc.)
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
  echo "Invalid Email.......";
  }else{
  $result = mysql_query("SELECT * FROM registration WHERE email='$email'");
  $data = mysql_num_rows($result);
  if(($data)==0){
  $query = mysql_query("insert into registration(name, email, password) values ('$name', '$email', '$password')"); // Insert query
  if($query){
  echo "You have Successfully Registered.....";
  }else
  {
  echo "Error....!!";
  }
  }else{
  echo "This email is already registered, Please try another email...";
  }
  }
  mysql_close ($connection);
  ?&gt;</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Squash Registration Form&lt;/title&gt;
  &lt;meta name="robots" content="noindex, nofollow"&gt;
  &lt;!-- Include CSS File Here --&gt;
  &lt;link rel="stylesheet" href="style.css"/&gt;
  &lt;!-- Include JS File Here --&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="registration.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div class="container"&gt;
  &lt;div class="main"&gt;
  &lt;form class="form" method="post" action="#"&gt;
  &lt;h2&gt;Squash Registration Form&lt;/h2&gt;
  &lt;label&gt;Name :&lt;/label&gt;
  &lt;input type="text" name="dname" id="name"&gt;
  &lt;label&gt;Email :&lt;/label&gt;
  &lt;input type="text" name="demail" id="email"&gt;
  &lt;label&gt;Password :&lt;/label&gt;
  &lt;input type="password" name="password" id="password"&gt;
  &lt;label&gt;Confirm Password :&lt;/label&gt;
  &lt;input type="password" name="cpassword" id="cpassword"&gt;
  &lt;input type="button" name="register" id="register" value="Register"&gt;
  &lt;/form&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>