cali_dotcom;10984663 wrote:
by using custom attr in the <a> tag:
... which makes your code invalid (unless you create a custom DTD).
cali_dotcom;10984663 wrote:
<a href="ratepost.php" postid="$post[postid]" vote="1" >
Apart from having invalid attributes 'postid' and 'vote', this link appears to send a get request for 'ratepost.php', while in reality it does not. Or do you actually send both an ordinary get request as well as a post request with ajax?
The href attribute is not a placeholder for javascript data. Here I recommend using a defined 'vote' function, and hardcoding ratepost.php into said function, since voting certainly relates specifically to this php script. Should you have a more generic javascript function, you'd pass the php script URL as a function parameter (or decide which script to run through some other means).
In my opinion it should be rewritten as
<a href="javascript:void(0);" onclick="vote(this, '$post[postid]');">
And the function definition
function vote(el, id)
{
/* I don't know what values you are using, but this sets
* vote_up = 1, vote_down = -1
*/
var vote = el.className == 'vote_up_button' ? 1 : -1;
$.ajax(
{
type: 'POST',
url: 'ratepost.php', /* now there's only one place to change
should you ever change the script url */
dataType: 'json',
data: {'postid': id, 'vote': vote},
success: function(data)
{
}
}
);
}
And personally I'd even add a third function parameter, vote, and explicitly specify 1 and -1 respectively, rather than rely on the CSS classname, since someone might some day change the cssname when changing the website design while having no idea that it will break javascript. I.e.
<a class="vote_up" onlick="vote(this, '$post[postid]', 1)" />
<a class="vote_down" onlick="vote(this, '$post[postid]', -1)" />
and
function vote(el, id, vote)