2013-11-15 21 views
-1

我使用的onClick

script1.php

<td><a href="#top_opps" onClick="test('hello');" role="button" data-toggle="modal"><?php echo $row['listings_views']; ?></a></td> 

传递变量通过href和这是我的Ajax代码

阿贾克斯函数

<script language="javascript" type="text/javascript"> 
function test(x){ 
    $.ajax({ 
    type: "POST", 
    url: "folder1/folder2/script2.php", 
    data: "var=x", 
    success: function(e){ 
    } 
    }); 
}; 
</script> 

但是当我想要得到的VAR值

script2.php

<?php 
$variable = $_POST['var']; 
echo $variable; 
?> 

变量为空

什么建议吗?

预先感谢您

+1

数据:“VAR =” + X, –

+0

如果你正确读出他的问题,“变量是空的”,所以他无法得到它。语法不正确,看看我的回答 –

+0

@GuillaumeLehezee'“var =”+ x'与“{var:x}”相同 –

回答

0

的语法不正确,尝试与这个jQuery:

<script language="javascript" type="text/javascript"> 
function test(x){ 
    $.ajax({ 
    type: "POST", 
    url: "folder1/folder2/script2.php", 
    data: { var: x }, 
    success: function(e){ 
    } 
    }); 
} 
</script> 

查看JQUERY website

0

exemples您加入x作为字符串的一部分,而不是一个变量。应该来说,

function test(x){ 
    $.ajax({ 
    type: "POST", 
    url: "folder1/folder2/script2.php", 
    data: "var="+x, // or data:{var:x}, 
    success: function(e){ 
    } 
    }); 
}; 
0

有几个问题,你的函数:

function test(x){ 
    $.ajax({ 
    type: "POST", 
    url: "folder1/folder2/script2.php", 
    data: "var=x", <-- change into "var="+x otherwise $_POST['var'] will be "x" 
    // or {var:x} will also work 
    success: function(e){ 
    } 
    }); 
}; <-- no semi-colon needed here