Reputation: 37
I'm having trouble multiplying the result from a database search from php in a jQuery script. I echoed the db result it's a double type in the database. So just a number, and I want it to be multiplied with a number and a variable that takes a number.
I tried doing this by adding the number and the variable in the jQuery output like so:
$.post('nutritional_value.php', {value:value}, function(data){
$('#search_result2').html(data*uneseno*10);
});
uneseno and 10, being the variable and number which I want it to multiply it with. Instead I just get the direct value from the database.
This is the .php file:
<?php
include 'connect.php';
$value = $_POST['value'];
$query = mysql_query("SELECT FAT FROM ccm WHERE NAME LIKE '$value%'");
while( $run = mysql_fetch_array($query)){
$fat = $run['FAT'];
echo $fat;
}
?>
This is the script:
<script>
function funkcija() {
var value = $('a').text();
$('#hidden1').show();
$('#jedinice_butt').click(function () {
var odabrano = $("#dropdown option:selected").text();
var uneseno = $("#input_jedinica").val();
if (odabrano === "g") {
$.post('nutritional_value.php', {
value: value
}, function (data) {
$('#search_result2').html(data * uneseno * 10);
});
}
});
}
</script>
The uneseno variable is a number when I enter it in the input field. Console doesn't show any errors or anything.
Am I missing something ?
Upvotes: 1
Views: 612
Reputation: 856
the problem is here:
var uneseno = $("#input_jedinica").val();
val() return a string...
Solution: use parseFloat()
$.post('nutritional_value.php', {
value: value
}, function (data) {
$('#search_result2').html(data * parseFloat(uneseno) * 10);
});
Upvotes: 1