Im having an issue where i can't seem to assign an ID from a column in the database to each DIV respectively.
The Scenario: I have a page full of clients and next to each one is a vote up and vote down button. When i click one of them it simply adds +1 to either the positive or negative column.
What is the Issue? The issue is no matter which client i vote for it only updates the ClientID '1'
What have i done so far?
- I am using a while loop to retrieve all the clients from the database. I have given each div that comes through a data-clientid for $_POST['ClientID'].
- I have classes called voteup and votedown so i can reference them in my JS script.
- In my script i assign the clientid from data-clientid to variables using the classes voteup and votedown
- In the voteup/down php files i then use this clientid to use the update sql to update the column.
What is my code?
My main page that displays the clients and the voting buttons (Shortened to the bit you need to see, connection to the database is included and all of that works fine)
<?php
$clientInfo = "SELECT * FROM Clients ORDER BY Client ASC";
$stmt = sqlsrv_query($conn, $clientInfo);
echo "<div style='width: 100%; display: inline-block;'>";
while ($client = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
{
echo "<div class='clientid' style='height: 50px; font-size: 18px; vertical-align: middle; display: inline-block; width: 100%'>" .
"
<div style='width: 35%;'></div>
<div onclick=\"voteUp()\" style='display: inline-block; width: 5%;'>
<span style='font-size: 20px;' class='hover-cursor fa fa-chevron-up vote-up'></span>
</div>" .
"<div class='hover-cursor hvr-underline-reveal voteup votedown' data-clientid='{$client['ClientID']}' style='width: 20%; display: inline-block;'>" . $client['Client'] . "</div>" .
"<div onclick=\"voteDown()\" style='display: inline-block; width: 5%;'>
<span style='font-size: 20px; text-align: right;' class='hover-cursor fa fa-chevron-down vote-down'></span>
</div>
<div style='width: 35%;'></div>
</div>
<br />";
}
echo "</div>";
?>
This then links to my scripts file with my JQuery (I set it up to print the ID in the console so i can see which ID it is hitting. Im just going to take Vote Up as an example)
window.voteUp = function() {
var clientid = $(".voteup").data("clientid");
$.post("voteup.php", {clientid: clientid}, function(data) {
console.log("Data:" + clientid)
});
return false;
}
and then there is the voteup.php that i am referring to in my $.post
<?php
if (isset($_POST['clientid'])) {
$voteup = "UPDATE Clients SET Pos = (SELECT MAX(Pos) FROM Clients) + 1 WHERE ClientID = " . $_POST['clientid'];
$stmt = sqlsrv_query($conn, $voteup);
} else {
echo "Failed";
}
?>