onClick $var++ not working
I am really new to PHP or any C based language. I've been trying to replace the Joomla rating system with thumbs and I can't ever get it to work.
<?php
$voteup = 0;
$vo开发者_开发技巧tedown = 0;
?>
<INPUT type="button" value="Vote Up" onClick="<?php echo $voteup++?>;">
<?php echo $voteup; ?>
It now shows 1 in the browser and returns nothing when clicked. It should be the other way. Why are these lines not effective? I've tried some other formats including a href = javascript and a submit button instead.
You need to use some sort of database to store your votes.
Variables aren't persistent across page loads, so what you're actually doing is setting the variables to 0 every time the page loads.
In addition, the PHP code you put in the onClick is always run, so you always increment $voteup
.
I recommend you take a look at guide, such as "HOW-TO: Reddit-style Voting With PHP, MySQL And jQuery", and see if that helps you get started.
Since you're working in Joomla, you'll need to access the database in a different way, however. For how you can do that, take a look at "How to use the database classes in your script". It has short examples on how to interact with the Joomla database layer. Then, whenever you find some MySQL-specific code, try to see if you can port it over to using those methods.
PHP is server-based, javascript is client-based. So this could never work.
What you need is sth. like AJAX -> http://www.ajaxf1.com/tutorial/ajax-php.html
also you need a DB to store the votes!
You are mixing up server-side and client side, the code the browser sees after it is parsed and sent by the server is something like:
<INPUT type="button" value="Vote Up" onClick="0">
1
just check the source.
You will need a javascript onClick handler that handles the client side so that when someone clicks, a request is made to the server to update your variable (that needs to be stored somewhere by the way) and the user is either re-directed to a new, updated page or you use javascript to update the content of the current page.
精彩评论