包括发布请求交换var的jQuery

问题描述

| 我有点受jQuery困扰。目前,我的功能如下所示。
$(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\\-\\.,@?^=%&amp;:/~\\+#]*[\\w\\-\\@?^=%&amp;/~\\+#]){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中)重写为带有我自己的url(例如
my.site?ref.PHP?id=unique12
)的重定向链接。我需要的是对PHP文件的POST请求(下面的代码),其中给出了有效的用户URL。作为var到PHP文件中,然后PHP文件应使用生成的唯一unique-ref-id来返回一个var。我当然知道上面的代码不能那样工作,它仅显示最终结果应该看起来像。PHP文件到此生成的unique-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)


?>
如果有人可以帮助我,那就太好了。非常感谢 :)     

解决方法

        使用POST jQuery \的方法。这是一个例子
$.post(\'URL-TO-POST-DATA\',{\'parameter1\': \'value\',\'parameter2\': \'value\'},function(data_received){
/** Here goes your callback function **/ alert(data_received);
});
有关POST方法的更多信息 不要忘记一件事。如果您不在PHP上使用echo(而不是return),jQuery将不会收到任何信息。您必须在PHP中使用echo,请不要忘记它。