2011-06-25 64 views
0

我有點卡住了jQuery。目前我的功能看起來像這樣。jquery包括post請求(交換變量)

$(function(){ 
    $(".url2").keyup(validNum).blur(validNum); 

    function validNum() { 

    var initVal = $(this).val(); 
    outputVal = initVal.replace(/(https?:\/\/)?(www.)?[0-9A-Za-z-]{2,50}\.[a-zA-Z]{2,4}([\/\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#]){0,250}(\s){1}$/ 
,"http://my.site/ref.php?id=<?php $code = unqiue-ref-id(); echo $unqiue-ref-id;?>"); 
    if (initVal != outputVal) { 
     $(this).val(outputVal); 
     return false; 

     }} 

}); 

所以現在它重寫用戶鍵入URL(在一個textarea)來重定向鏈接與我自己的網址(如my.site?ref.php?id=unique12。我需要的到底是一個POST請求的PHP文件(下面的代碼),其中有效的用戶url是作爲一個var提供給php文件的,然後php文件應該返回一個帶有生成的唯一unique-ref-id的var,我當然知道上面的代碼不是那樣工作的,這隻能說明最終的結果應該是什麼樣子等。PHP文件至極生成唯一-REF-ID看起來是這樣的。

function unqiue-ref-id() { 
    $unqiue-ref-id = ""; 
    $lenght=4; 
    $string="abcdefghijklmnopqrstuvwxyz123456789"; 

    mt_srand((double)microtime()*1000000); 

    for ($i=1; $i <= $lenght; $i++) { 
     $shorturl .= substr($string, mt_rand(0,strlen($string)-1), 1); 
    } 
    return $unqiue-ref-id; 

} 


$user-url = // var send from jquery 


do{ 
    $unqiue-ref-id = unqiue-ref-id(); 
} while(in_array($unqiue-ref-id, $result)); 

// var send back to jquery function => final results of do function above 

// Here i add the $user-url and $unique-ref-id to datebase (Dont need code for that) 


?> 

會如此巨大的,如果有人能幫助我與。非常感謝:)

回答

0

使用POST jQuery的方法。這裏有一個例子

$.post('URL-TO-POST-DATA', {'parameter1': 'value', 'parameter2': 'value'}, function(data_received){ 
/** Here goes your callback function **/ alert(data_received); 
}); 

More information about POST method

不要忘了一兩件事。如果你沒有在PHP上使用echo(而不是返回),jQuery將不會收到任何迴應。 你必須在PHP中使用echo,不要忘記它。