0

Hello I am begginner in php.

I create a ecommerce site .

first I fetch all information from database. all is going correct but I have problem in quantity input

when I insert quantity value then the total calculate in total .

I use js but I don't know to fetch the price value from database and store in javascript variable.

the code is :

<?php

                echo "ISBN:". $row['isbn']."<br/>";
                echo "Publisher:". $row['publisher']."<br/>";
                echo "Year:". $row['year']."<br/>";
                echo "Price:". $row['price']."<br/>";
                ?>
Qty : <input type="text" name="qty1" id="qty"/><br>


Total : <input type="text" name="total" id="total"/>

<a href="javascript:sumInputs()">Sum</a>


<script>
window.sumInputs = function() {
    var inputs = document.getElementsByTagName('input'),
        result = document.getElementById('total'),
        sum = 0;            
    var q = $row['price']; //how to resolve this line


    for(var i=0; i<inputs.length; i++) {
        var ip = inputs[i];

        if (ip.name && ip.name.indexOf("total") < 0) {
            sum = parseInt(ip.value)*q || 0;
        }

    }

    result.value = sum;
}


</script>
Sandesh
  • 1,190
  • 3
  • 23
  • 41
user3106845
  • 1
  • 1
  • 1
  • Well , you can make an AJAX request to your service , and store the response which comes from service into a vaiable in your callback function. – Harsha Venkataramu Dec 16 '13 at 10:18

1 Answers1

0

you can combine javascript and php functionality by simply telling the parser to "print into the javascript". when the html file is "viewed" by the client, the javascript is beeing executed.

var q = parseFloat(<?php echo $row['price']; ?>);

pay attention that $row['price'] contains a number with a . as decimal dot - like 10.33 and the line

; ?>;

fist ; is the end from the php variable declation and second ; - after ?> is to end the variable declation from javascript.

Note - this only works when the Javascript Code an the HTML content is on the same Page. When dealing with an external Javascript Source - write in your PHP something like this

echo '<script>var q = "' . $row['sum'] . '";</script>";

and then use q in your external js file

ins0
  • 3,918
  • 1
  • 20
  • 28