通过jquery和ajax发布两个变量

时间:2012-05-07 01:04:49

标签: php javascript jquery ajax

在按下确认窗口上的按钮时,我无法使用ajax和jquery发布两个变量。我可以获得任一个变量来单独显示,但是当我尝试同时发布两个变量时它将无法工作。

编辑 - 问题解决了。没有包含我需要的文件。我的错!

echo '<input type="button" value="Delete" onclick="deleteSomething(\'' . $replyID .'\', \'' .$tableName. '\',\'' .$replyBy. '\')" />';
?>
<script type="text/javascript">

function deleteSomething(replyID, tableName, replyBy)
{
if (!confirm("Are you sure?"))
    return false;
$.post('pageprocessing.php',"replyID=" + replyID + "&tableName=" + tableName + "&replyBy=" + replyBy, function(response) {
    alert(response);
});
}

这是我在pageprocessing.php上的脚本。我发布的所有三个值都正确回显,我只是无法弄清楚为什么它们没有被删除。

if(isset($_POST['replyID']) && isset($_POST['tableName']) && isset($_POST['replyBy'])){

    if($_POST['tableName'] == "replies" && $_POST['replyBy'] == $userid){
        echo $replyID = $_POST['replyID']; //echoing variables to make sure the page gets this far
        echo $replyBy = $_POST['replyBy'];
        echo $tableName = $_POST['tableName'];
        mysql_query("delete from replies where repliesID = $replyID "); //I can type this query into terminal and it deletes. Why won't it delete from here?
    }
}

2 个答案:

答案 0 :(得分:3)

你的帖子的变量都应该连接起来。

$.post('pageprocessing.php',"replyID=" + replyID + "&tableName=" + tableName, function(response) {
    alert(response);
});

您也可以使用对象

$.post('pageprocessing.php',{
    replyID     : replyID, 
    tableName   : tableName
}, function(response) {
    alert(response);
});

答案 1 :(得分:0)

不需要等号!像这样使用它

$.post('pageprocessing.php',{ replyID : replyID, tableName :  tableName}, function(response) {
    alert(response);
});