1

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 ?

2
  • 1
    you can try (parseInt(uneseon)*parseInt(data)*10). Commented May 24, 2013 at 13:41
  • @mehdinejati I get 0 as the output, no mather what number I enter in uneseno Commented May 24, 2013 at 13:51

1 Answer 1

1

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);
        });
Sign up to request clarification or add additional context in comments.

4 Comments

maybe data is returning 0..., use an alert(data + "," + uneseno) to check wich is 0
nope, it alerts the value from DB and the one I entered. (0.3846,111)
fixed it! $('#search_result2').html(data * parseInt(uneseno,10)); Thank you :)
I updated my answer, if $("#input_jedinica") can receive decimal numbers, you have to use parseFloat() (parseInt will truncate to integer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.