好的,您提到您是“初学者”(仅供参考,我也不是专业开发人员),但我假设您知道表单是什么以及它们如何工作。下面是超级简单的,我什至不会使用 AJAX。(评论中的解释。)
代码将在一个文件中。您提到了 PHP,所以我假设您可以使用它。这就是我在下面使用的:
<?
if (isset($_POST['vote'])) { // Check if there is a vote POSTed to our page
// Store the vote. I don't know how you did it the previous time, I'm just going to write it to a text file
$file = fopen("votes.txt", "w");
fwrite($file, $_POST['vote']);
fclose($file);
}
?>
<!-- the voting page -->
<HTML>
<HEAD>
<title>Vote</title>
</HEAD>
<BODY>
<!-- Create a form to be able to send the vote to the server in the simplest way, but don't display it -->
<form action="thispage.html" method="post" style="display:none;">
<!-- I don't know what possible values there are. I'll just take 'foo' and 'bar'. Of course you can add more. -->
<input type="radio" name="vote" value="foo" />
<input type="radio" name="vote" value="bar" />
</form>
<!-- The images representing the (invisible) radio button -->
<!-- I use the data-value attribute to store to which radio button this image corresponds -->
<img src="path/to/foo/image" data-value="foo" />Vote FOO<br />
<img src="path/to/bar/image" data-value="bar" />Vote BAR<br />
<!-- Import jQuery for the sake of simplicity. -->
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<!-- The trickiest part. The script. -->
<script>
$("img").click(function() {
var value = $(this).data('value'); // Get the value
$("input[value='" + value + "']").click();// Click the corresponding radio button
$("form").submit(); // Submit the form.
});
</script>
</BODY>
</HTML>
未测试。