title: return json_encode error from php to jquery
body: <p>i want to display error if username and pass is wrong.i am trying from yesterday. dont know whats wrong.every time controll goes in error and displays error.tried json.but not working.i an new in jquery ajax,    </p>
  
  <pre><code>&lt;script&gt;
  
  $(document).ready(function() {
      $("#LoginForm").submit(function(e) {
          $("#simple-msg1").html("&lt;img  src='img/loading.gif'/&gt;");
          var postData ="";
          postData = $('#LoginForm').serializeArray();
          var formURL = $('#LoginForm').attr("action");
  
          $.ajax( {
              url : formURL,
              type: "POST",
              data : postData,
              dataType:'json',
              success:function(data, textStatus, jqXHR)  {
                  alert(data.error);
                  if(data.error == 1) {
                      $("#simple-msg1").html('&lt;pre&gt;&lt;code class="prettyprint"&gt;'+data.message+'&lt;/code&gt;&lt; /pre&gt;');
                  } else {
                      $("#simple-msg1").html('&lt;pre&gt;&lt;code class="prettyprint"&gt; Login Successfull &lt;/code&gt;&lt;/pre&gt;');
                      window.location = "/property/Dealer/ManageProfile.php?Login=successfull";
                  }
              },
              error:function(data,textStatus)  {
                  $("#simple-msg1").html('&lt;pre&gt;&lt;code class="prettyprint"&gt; wrong username or password 11&lt;/code&gt;&lt;/pre&gt;');
              }
          });
          e.preventDefault(); //STOP default action
      });
  
      $("#Button1").click(function() {
          $("#LoginForm").submit(); //SUBMIT FORM
      });
  
  });
  
  &lt;/script&gt;
  </code></pre>
  
  <p>this is php file </p>
  
  <pre><code>&lt;?php
  ob_start();
  
  if(session_id() == '')
      {
          session_start();
      }
  
  include 'config.php';
  
  $error = '0';
  $message = 'Valid';
  $redirect = 'Dealer/EditLoginDetails.php';
  
  $myusername=$_POST['txtusername']; 
  $mypassword=$_POST['txtpassword']; 
  
  $myusername = stripslashes($myusername);
  $mypassword = stripslashes($mypassword);
  
  $myusername = mysql_real_escape_string($myusername);
  $mypassword=mysql_real_escape_string($mypassword);
  
  $qry = "SELECT UserName,Type_user FROM login WHERE UserName = '".$myusername."' AND password = '".$mypassword."' ";
  
  $result = mysql_query($qry) or die ("Query failed");
  
  $UserData = mysql_fetch_array($result);
  
  if($UserData['UserName'] != "") {
      //echo $UserData['UserName'];
  
      $_SESSION['UserId'] = $myusername;
  
      $typ = $UserData['Type_user'];
  
      if ( $typ == "Dealer") { 
          header('location:/Dealer/EditLoginDetails.php');  
          //echo "dealer";
          //echo json_encode(array('success'=&gt;'true'));
          //header('location:/Dealer/EditLoginDetails.php');
      } else if ($typ == "Individual") {
          header('location:/Dealer/EditLoginDetails.php');  
  
      } else {
          header('location:/Builder/managep.php'); 
      }
  
  } else { 
      $error = '1';
      $message = 'Invalid username or password';
  
      // echo "forbiddnt";
      //header('HTTP/1.0 403 Forbidden');
      //echo " wrong username or password";
  }
  echo json_encode(array('error' =&gt; $error, 'message' =&gt; $message, 'redirect' =&gt; $redirect));
  
  ?&gt;
  </code></pre>