title: Return php error from Ajax call
body: <p>I have a php script and I would like it to be called with ajax, I have had an error coming from the ajax which says "Error:email=my_email&amp;password=myPassword".</p>
  
  <p>Here is the PHP script</p>
  
  <pre><code>&lt;?php
  
  session_start(); //starts a session in order to be-able to save session variables and to read them
  
  require "db_config.php"; //Allows us to use the database connection from db_config.php in this file
  
  if ($_SERVER["request_method"] == "post"){ //checks if the form was submitted
  
      $email = $_POST["email"]; //fetching the email address which was inserted in the login.html form
      $password = $_POST["password"]; //fetching the password which was inserted in the login.html form
  
      /*
  
      querying the database, to check whether there is a result with the email and password entered by the user
  
      */
  
      $checkForUser = mysqli_query($db_connection, "SELECT * FROM `tbl_users` WHERE email = '$email' and Password = '$password' LIMIT 1");
  
      /*
  
      checking if the query resulted in one row, if there is a row
      it means there is a user with this email and password, which means these are the correct creadentials
  
      */
  
      $rows = mysqli_num_rows($db_connection, $checkForUser);
  
      if ($rows == 1){ 
  
          //this means: correct credentials
  
          //the next few lines fetch the information from the result
          while($row = mysqli_fetch_assoc($checkForUser)){
              $_SESSION["user_id"] = $row["userId"]; //creates a session variable containing the users id
              $_SESSION["users_name"] = $row["firstName"]. " ".$row["lastName"]; //creates a session variable containing the users name
          }
  
          echo "You are now logged in: ". $_SESSION["users_name"];
  
      }else{
  
          //this means: incorrect credentials
          echo "Incorrect Username or password"; //prints out error message
  
      }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>Here is main.js</p>
  
  <pre><code>$(document).ready(function() {
  
      $('#loginForm').submit(function() {
          var data = $(this).serialize();
  
          $.ajax({
          url: "../php/login.php",
          type: "POST",
          data: data,
              success: function(data) {
                  $('*').html(data);
              },
              error: function() {
                  alert('ERROR: ' + data);
              }
          });
  
          return false;
  
      });
  
  });
  </code></pre>
  
  <p>Here is the login.html page which may be helpful</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;link rel="stylesheet" type="text/css" href="css/main.css"&gt;
      &lt;link rel="stylesheet" type="text/css" href="css/responsive.css"&gt;
      &lt;script src="js/jquery.js"&gt;&lt;/script&gt; 
      &lt;script src="js/main.js"&gt;&lt;/script&gt;
      &lt;meta name="copyright" content="Yudi Moszkowski"&gt;
      &lt;title&gt;Login | Your Site Name&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;div id="loginContainer"&gt;
          &lt;div class="logo"&gt;&lt;img src="img/yourLogo.png"&gt;&lt;/div&gt;
          &lt;form action="php/login.php" method="post" id="loginForm"&gt;
              &lt;input type="text" name="email" placeholder="Email" id="loginEmail" class="loginInput" required="true"&gt;
              &lt;input type="password" name="password" placeholder="Password" id="loginPassword" class="loginInput" required="true"/&gt;
              &lt;input type="submit" value="Login" name="loginSubmit" id="loginSubmit"&gt;
          &lt;/form&gt;
          &lt;div id="loginOptions"&gt;&lt;p id="noAccount"&gt;Not signed up? &lt;a href="signup.html"&gt;Signup&lt;/a&gt;&lt;/p&gt;&lt;p id="forgotPass"&gt;&lt;a href="forgot_pass.html"&gt;Forgot password?&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks for your time :)</p>