Barnold Barnold - 5 months ago 14
MySQL Question

Is there a 'cleaner' way I can assign a MySQL value to a jQuery variable?

I am creating a basic auction site and having a slight issue where when a bid is placed, it sometimes says it's too low, despite it being higher than the current bid. I think this is to do with the way I am getting the current bid, as it's not a very tidy approach in my opinion.

So my HTML/PHP which retrieves and lists the auctions on the page:

$result = mysqli_query($con,"SELECT * From auction WHERE category = 'Bathroom' ORDER BY ID DESC");

while($row = mysqli_fetch_array($result))
echo "<form name='auction' id='auction" . $row['ID'] . "'>
<input type='hidden' name='id' value='" . $row['ID'] . "' />
<div class='auction-thumb'>
<div class='auction-name'>" . $row['Item'] . "</div>";
echo "<img class='auction' src='" . $row['ImagePath'] . "' />";
echo "<div class='auction-bid'>Current Bid: £<div class='nospace' id='" . $row['ID'] . "'>" . $row['CurrentBid'] . "</div></div>";
echo "<div class='auction-bid'>Your Name: <input type='text' class='bidder' name='bidname' autocomplete='off'/></div>";
echo "<div class='auction-bid'>Your Bid: <input type='text' class='auction-text' name='bid' autocomplete='off'/></div>";
echo "<div class='auction-bid'><input type='submit' name='submit' value='Place Bid!' /></div>";
echo "<div class='bid-success' id='bid" . $row['ID'] . "'>Bid placed!</div>";
echo "</div></form>";

As you can see, I wrap the column/value "CurrentBid" in a div with the ID of the ID in MySQL.

Then when someone places a bid, the following jQuery/AJAX code is called:

var id = $(this).find('input[name="id"]').val();
var bidname = $(this).find('input[name="bidname"]').val();
var bid = $(this).find('input[name="bid"]').val();
var currentbid = $('#'+id).text();

var itemdesc = $(this).find('.auction-name').text();

if (bidname == '')
alert("No name!")
return false;

if (bid > currentbid)
alert("Bid is greater than current bid");
alert("Bid is too low!");
return false;

type: "POST",
url: "auction-handler.php",
data: {bidname: bidname, bid: bid, id: id, itemdesc: itemdesc},
success: function(data){
$('#bid'+id).fadeIn('slow', function () {
return false;


As you can see from this code, I assign the variable 'currentbid' by selecting the ID of the div container, and then pulling through the text within it.

I am not sure if this is what is causing me the problem, but it seems likely, I cannot figure out why sometimes it says "Bid too low" despite me putting in a higher price then what is currently in the Current Bid div.

Ideally, I'd like to assign the jQuery variable 'currentbid' with the value directly from MySQL, but I am not too sure if this is possible.

Does anyone know of a way that I can do this? Or is there a better way I can assign a value to the variable?

Thank you

svn svn

You are comparing strings now instead of numeric values. You should cast your values to floats before comparing.

bid = parseFloat(bid)
currentbid = parseFloat(currentbid);

Also on the server side when processing to bid you should first check the currentBid from the database because it could have changed already.