从php到ajax的数据发布

时间:2013-12-15 06:36:45

标签: php jquery mysql ajax

我正在使用这个jquery我想将数据从php发送到jquery但它不工作。如果我不使用json它工作正常。但我希望json知道哪个用户有登录天气他是建设者,经销商或个人我是jquery的新手

<script>

  $(document).ready(function()
  {
    $("#LoginForm").submit(function(e)
    {

      $("#simple-msg1").html("<img  src='img/loading.gif'/>");
      var postData ="";
      postData = $(this).serializeArray();
      //alert(postData);

      var formURL = $(this).attr("action");

      $.ajax(
      {
        url : formURL,
        type: "POST",
        data : postData,
        contentType: "application/json; charset=utf-8",
        dataType:"json",
        success:function(data,textStatus,jqXHR)
        {
          alert("successss");
          $("#simple-msg1").html('<pre><code class="prettyprint"> Login Successfull </code></pre>');

          if(data.message == 'Dealer')
          {
            // window.location = "Dealer/EditLoginDetails.php?Login=successfull";
            alert("Wellcome Dealer");
          }
          else if(data.message == 'Individual')
          {
            alert("Wellcome Individual");
          }
          else if(data.message == 'Builder')
          {
            alert("Wellcome Builder");
          }




          //return true;
        },
        error:function(jqXHR, textStatus, errorThrown)
        {
          alert("failer");
          $("#simple-msg1").html('<pre><code class="prettyprint">  </code></pre>');

          $("#simple-msg1").html('<pre><code class="prettyprint"> wrong username or password </code></pre>');
        }
      });
      e.preventDefault(); //STOP default action
    });

    $("#Button1").click(function()
    {

      $("#LoginForm").submit(); //SUBMIT FORM
    });

  });

</script>

这是检查登录页面

<?php
header("Content-Type: application/json");
ob_start();

if(session_id() == '')
                 {
                   session_start();
                }


//include 'CUserDB.php';


include 'config.php';
 $error = "success";
    $message = '';
    $redirect = '';


$myusername=$_POST['txtusername']; 
$mypassword=$_POST['txtpassword']; 

//$myusername="deepak"; 
//$mypassword="deepak"; 


$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);
$mypassword=mysql_real_escape_string($mypassword);

$str="select VerificationCode from user_verification where UserName='".$myusername."'";
$result=mysql_query($str) or die ("Queryfailed");
$UserData=mysql_fetch_array($result);
//if($UserData['VerificationCode']!='' && $UserData['VerificationCode']!=NULL)
//{
    //echo "<script type='text/javascript'> alert('Please Enter the verification code first.') ";
    //echo "<script>  window.location.href='VerifyUser.php?UName=".$Uname."'"; 


 // return 0;   
//}
//else
//{         
$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'];

          $_SESSION['Typeuser'] = $UserData['Type_user'];


    if($typ == "Dealer")
    { 
    $message = "Dealer";

    //  header('location:Dealer/EditLoginDetails.php');


    }
    else if($typ == "Individual")
    {
$message = "Individual";
         //header('location:Individual/EditLoginDetails.php');  

    }
    else if($typ == "Builder")
    {
    $message = "Builder";

        //header('location:Builder/ManageProjects.php'); 
    }



    }
   else
    { 


    header('HTTP/1.0 403 Forbidden');
echo "wrong username or password";
    }


    echo json_encode(array('error' => $error, 'message' => $message, 'redirect' => $redirect));
//}
?>

1 个答案:

答案 0 :(得分:0)

在我认为的脚本中不需要这一行,

contentType: "application/json; charset=utf-8",

试试..

同样在php页面中,内容类型标题是不必要的..