1

Can you see anything that might cause this to not work?

I have a MYSQL connection, the variables are right. I have <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> inside my head. What next?

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>

Thanks alot hope you can help a noob

Tomek
  • 11
  • 1
  • 1
    Other than the very visible SQL injection you left yourself open to, what's happening that you have a problem with? – Jess Jun 08 '11 at 21:12
  • The obvious thing is that you're not actually sending any data. – lonesomeday Jun 08 '11 at 21:13
  • use $_GET["action"] for header location variables – John Jun 08 '11 at 21:14
  • 1
    What's the error? Also, bit unrelated to your question, but for the safety of your site, have a read about sql injections, for example http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php – Niklas Jun 08 '11 at 21:15

4 Answers4

2

it appears your ratePost needs some more parameters, as well as to make use of those parameters. Also, there seems to be a syntax error in the onclick of your link.

onclick="ratePost('postID=[id from php]', rating=[rating from php], [id from php]);return false;"

rating=[rating from php] should probably be '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>
John
  • 1,309
  • 2
  • 12
  • 24
aepheus
  • 7,827
  • 7
  • 36
  • 51
  • edited for you :) but yea the OP is including jQuery library and simply not using the .post functionality which boggles the mind! – John Jun 08 '11 at 21:29
1

You need to send the data to the ajax call.

Here's a sample from the jquery docs:

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

You're not actually posting any content to that URL, so $postID and $rating are probably null or undefined or however PHP handles that.

Here's the syntax you're probably looking for:

$.ajax({
   type: "POST",
   url: "ajax.php?action=ratePost",
   data: //Content Here
 });
Robert
  • 21,110
  • 9
  • 55
  • 65
0

for a jquery post

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>
John
  • 1,309
  • 2
  • 12
  • 24