2013-12-15 53 views
0

我使用這個jquery我想從PHP發送數據到jquery,但它不working.if我不使用json它工作正常。但我想讓json知道哪個用戶登錄天氣他是我在jQuery的從PHP到ajax的數據文章

<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)); 
//} 
?> 
+0

什麼錯誤信息,您在AJAX的故事嗎? AJAX響應是什麼樣的? –

+0

「它不工作」 - 什麼不工作?你有錯誤嗎?你能理清哪部分代碼導致了這個問題? – sbking

+1

我的PHP技能是生鏽的。我一直在使用python。如果我沒有弄錯,json要求你添加它作爲參數,因爲json只是javascript的格式。您需要將用戶添加到數據中的ajax調用中。 python有一種方法可以從帖子中獲取json數據,然後檢查參數。可能從這開始[post](http://stackoverflow.com/questions/10955017/sending-json-to-php-using-ajax) –

回答

0

這條線是在腳本中,我認爲不需要,

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

嘗試的..在PHP頁面

此外,內容類型標題是不必要..

+0

試過這個不行 – dvirus