2010-12-06 93 views
0

我想創建一個簡單的登錄系統,將具有一個域(我們稱之爲域A)和另一個域(域B)上的PHP文件的形式和一切。爲什麼?只是因爲域A更可靠,但不支持PHP。我對所有這些AJAX的東西都很陌生,所以我正在使用我在網上找到的資源。jquery跨域與PHP的AJAX通信

了jQuery和表單上域A它的一部分是如下:

<form id="submit" method="post"> 
<fieldset> 
<legend>Enter Information</legend> 

<label for="fname">Name:</label> 
<input class="text" id="fname" name="fname" size="20" type="text" /> 

<label for="lname">Email:</label> 
<input class="text" id="lname" name="lname" size="20" type="text" /> 

<button class="button positive"> Submit </button> </fieldset> 
</form> 
<script> 
$(document).ready(function(){ 
    $("form#submit").submit(function() { 
    // we want to store the values from the form input box, then send via ajax below 
    var fname  = $('#fname').attr('value'); 
    var lname  = $('#lname').attr('value'); 
     $.ajax({ 
      type: "POST", 
      url: "http://domainB.com/somefolder/ajax.php", 
      data: "fname="+ fname +"& lname="+ lname, 
      success: function(){ 
       $('form#submit').hide(function(){$('div.success').fadeIn();}); 

      } 
     }); 
    return false; 
    }); 
}); 
</script> 

請注意,「L-NAME」和「FNAME」是要代表姓名和電子郵件,而不是名字和姓氏分別。

域B的PHP代碼:

<?php 

    // CLIENT INFORMATION 
    $uname  = htmlspecialchars(trim($_POST['fname'])); 
    $umail  = htmlspecialchars(trim($_POST['lname'])); 
    $uip = 12345; 
    $usecret = "secret"; 
//Mysql 
$con = mysql_connect("mysql.domainB.com","username","password"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 
//Connect 
mysql_select_db("login_db", $con); 
//Insert user details irrespective of whether the user is a member or not to make a log kind of thing 
mysql_query("INSERT INTO log_data (Name, Email, IP_ADDRESS, Secret) 
VALUES ('$uname', '$umail', '$uip', '$usecret')"); 

//Now check if the user is a subscriber or not 


// mysql_query("SELECT Email, Secret FROM login_data WHERE Email='$umail' AND Secret="secret""); 
//I don't know the code to check if the user has logged in correctly 
//This is where I need you to help me 
//I need to check if the user has logged in correctly, if yes, then return a message saying //success which should be caught by Jquery and displayed as an Alert, or something 
//If the user login failed, this php code should return a failure message which again should be caught by Jquery on Domain A and displayed as an alert in the least. 


mysql_close($con); 
//end Mysql 

?> 

事情,這裏要注意 - 有在login_db內的兩個表數據庫 - log_data存儲日誌信息和login_data這存儲用戶名和密碼。變量$ secret存儲用戶的密碼。

log_data插入的工作方式就像一個魅力,但它的login_data是困擾我。任何幫助將非常感激。謝謝!

的Imag

+0

@imaginonic,是你的PHP工作jquery ajax的相關工作,做一件事只是執行php和看到e一切正常工作 – kobe 2010-12-06 04:36:20

+0

@gov - 謝謝gov,對於答覆,第一個查詢(插入)工作得很好,我只需要讓選擇查詢(第二個查詢)工作。謝謝 – dsignr 2010-12-06 04:40:10

+0

@imaginonic,如果你的插入工作,那麼它不是一個跨域問題的權利,你的選擇queryu? – kobe 2010-12-06 04:42:33

回答

0

只要給一個嘗試下面的代碼...它應該照顧跨域問題也

var dataString = "fName=" + $('#fname').attr('value') + "&lname=" + $('#lname').attr('value'); 
       var url = "http://demo/exampe.php?" + dataString; 


       $.getJSON(url + "&jsoncallback=?", function(data){ 

       alert("success"); 

JSONP對象的構造應該是如下。

{"menu": { 
    "id": "file", 
    "value": "File", 
    "popup": { 
    "menuitem": [ 
     {"value": "New", "onclick": "CreateNewDoc()"}, 
     {"value": "Open", "onclick": "OpenDoc()"}, 
     {"value": "Close", "onclick": "CloseDoc()"} 
    ] 
    } 
}} 



       }); 
0

你應該使用的數據類型JSONP

$.ajax({ 
      url: url, 
      async: true, 
      type: "GET", 
      dataType: "jsonp", 
      success: function(json){ 
       // handle success response here 
      }, 
      error: function(){ alert("x.err"); } 
     });