So, I thought I had this figured out, but, nope. So I could use any help here.
I have a html page. On that page I have three links. Each link representing a different piece of data. When a user clicks those links it will then post to a PHP page and carry that data to the PHP page. The PHP page will then update a database. Then, the PHP page will return the updated results BACK to the HTML page.
I know this requires JQuery, PHP, and Ajax.
Here is what I NOW have with some help from the boards:
HTML PAGE
<script src="_js/jquery-1.7.2.min.js"></script> <!-- Linking jQuery -->
<script>
$(document).ready(function () {
$('.answer').click ( function (e) {
var color = $(this).attr("data-color");
$.ajax({
url: 'mm.php',
type: 'POST',
data: '{ color: "'+color+'" }',
success: function (res) {
...
},
error: function (jqXHR) {
...
}
})
})
}
</script>
<title>M&M Poll</title>
</head>
<body>
<h1>VOTE FOR YOUR FAVORITE COLOR M&M</h1>
<h2>Click the M&M to vote</h2>
<div id="wrapper">
<div id="red" data-color="red" class="answer">
<a href="#"><img src="images/red.jpg" width="100%" /></a>
</div>
<div id="blue" data-color="blue" class="answer">
<a href="#"><img src="images/blue.jpg" width="100%" /></a>
</div>
<div id="green" data-color="green" class="answer">
<a href="#"><img src="images/green.jpg" width="100%" /></a>
</div>
<div id=rvotes>
TEST
</div>
<div id=bvotes>
TEST
</div>
<div id=gvotes>
TEST
</div>
PHP Page
<?php
function showVotes()
{
$sql = "SELECT * FROM mms";
$result = mysql_query($sql) or die(mysql_error());
$showresult = mysql_query("SELECT * from mms") or die("Invalid query: " . mysql_error());
while ($row = mysql_fetch_array($showresult))
{
echo ("<br> M&M = ". $row["color"] . " has " . $row["votes"] . "votes <br>");
}
}
function addVote()
{
$sql= "UPDATE mms SET votes = votes+1 WHERE color = 'red'";
$result= mysql_query($sql) or die(mysql_error());
return $result;
}
?>
I know my database works. I just need to connect the HTML/AJAX/PHP
Any help is super appreciated!!