title: jQuery $.post() and PHP if(isset($_POST)) help optimizing
body: <p>I have a site that is to create a user account. The site currently works well, however i want to know how to improve and fix an issue i have. If the site sends the post to the server side php it creates the user correctly, however if someone loads the server php file without posting, it will create a user into the db with all values blank. I am doing some checking for users but please help me optimize my code. Thank you in advance.</p>
  
  <pre><code>/// HTML ///
  
  &lt;form id="form_customer"&gt;
      // form stuff here, please note my button for submit is not in the form but outside
  &lt;/form&gt;
  
  &lt;button id="form_submit"&gt;Submit&lt;/button&gt;
  &lt;button id="form_cancel"&gt;Cancel&lt;/button&gt;
  
  //Javascript jQuery
  
  $("#form_cancel").click(function() {
    window.location = 'index.php';
  });
  
  
  $("#form_submit").click(function() {
  $.post("incl/server_register.php", payload, function(data) {
      if(data == "500"){
          alert("An Error has occured.");
      }
      if(data == "200"){
          alert("Registration complete. Redirecting...");
          indow.location = 'index.php';
      }
      if(data == "400")
          alert("Username already exists. Please select another.");
  },"json");
  
  // And here is my server side PHP
  $mysqli = new mysqli($dbHost, $dbUser, $dbPass);
  $mysqli-&gt;select_db($dbDB);
  
  // Check connection
  if($mysqli-&gt;connect_error) {
      die('Connection Error: (' .$mysqli-&gt;connect_errno .') ' .$mysqli-&gt;connect_error);
  }
  
  if(isset($_POST))
  {
      // Query DB 
  $check = "SELECT * FROM table WHERE Username='$sUsername'";
  $result = $mysqli-&gt;query($check);
  
  if($result-&gt;num_rows == 0)
      {
          $query = "INSERT into table (blah blah)
          $result = $mysqli-&gt;query($query);
      if($result == false)
          echo json_encode("500");
      else {
          echo json_encode("200");
          mail($to, $subject, $message, $headers);
      }
  }
  else
      echo json_encode("400");
  }
  }
  </code></pre>