2011-06-08 93 views
1

你能看到任何可能导致此问题无法解决的问题吗?麻烦与ajax.Shouldnt这项工作?

我有一个MYSQL连接,变量是正确的。我脑子里有<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>。接下来是什么?

javascript: 

function ratePost(id) { 
    $.ajax({type: "POST", url: "ajax.php?action=ratePost"}); 
} 


ajax.php?action=ratePost: 

$postID = $_POST['postID']; 
$rating = $_POST['rating']; 
mysql_query("INSERT INTO userpostratings (postID, rating) VALUES ($postID, $rating)"); 


<a href="#" alt="+ (Up Vote)" class="vote" onclick="ratePost('postID=<?=$post['id'] ?>', rating=<?=$post['rating']?>, <?=$post['id'] ?>);return false;" rel="nofollow" title="Up vote this post">+</a> 

非常感谢希望你能帮助一个noob

+1

除了您自己打开的非常明显的SQL注入外,发生了什么问题? – Jess 2011-06-08 21:12:53

+0

显而易见的是你并没有发送任何数据。 – lonesomeday 2011-06-08 21:13:49

+0

使用$ _GET [“action”]作为标题位置变量 – John 2011-06-08 21:14:08

回答

1

您需要将数据发送到Ajax调用。

下面是来自jquery docs样本:

$.ajax({ 
    type: "POST", 
    url: "some.php", 
    data: "name=John&location=Boston", // this line is important 
}); 
0

您实际上并不发表任何内容,即URL,所以$postID$rating可能是零或未定义或PHP但处理该。

这里是你可能寻找的语法:

$.ajax({ 
    type: "POST", 
    url: "ajax.php?action=ratePost", 
    data: //Content Here 
}); 
0

为一个jQuery后

function ratePost(idVal,ratingVal) { 
$.post("ajax.php?action=ratePost", {rating: idVal, postID: ratingVal}, function(data){alert(data+" return val"); }); 
} 

<a href="#" alt="+ (Up Vote)" class="vote" onclick="ratePost('<?php echo $post['id']; ?>','<?php echo $post['rating']; ?>');return false;" rel="nofollow" title="Up vote this post">+</a> 
2

看来你ratePost需要更多的参数,以及利用这些参数。此外,您的链接onclick似乎有一个语法错误。

onclick =“ratePost('postID = [id from php]',rating = [来自php的评级],[来自php的id]); return false;”

rating = [来自php的评级]应该可能是'rating = [rating from php]'。

function ratePost(id,rating) { 
    $.post("ajax.php?action=ratePost", {postID: id, rating: rating}, function(data){alert(data+" return val"); }); 
    } 

    <a href="#" alt="+ (Up Vote)" class="vote" onclick="ratePost('<?=$post['id'] ?>', '<?=$post['rating'] ?>');return false;">+</a> 
+0

为你编辑:)但是是的OP是包括jQuery库,而根本就没有使用.post功能,这让boggles头脑! – John 2011-06-08 21:29:03