Using js with php- need help with code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Using js with php- need help with code

The purpose of this code is when the button is clicked , the price of the product is taken from the database and added added to the sum and so on. It basically gives you the total price of the products. The price is not displaying , can someone tell me why please. (i know some tags are missing like the body and style tags, but i have them in my actual code and its still not working) Heres my code: <body> <h1>Shop</h1> <h2>This is the shop page</h2> <?php $servername = "Minato"; //changed for safety reasons $username = "naurto";//changed for safety reasons $password = "Shippuden";//changed for safety reasons $dbname = "shop"; //changed for safety reasons $conn = new mysqli($servername, $username, $password, $dbname); $sql="SELECT * FROM products where productID=1"; $result = mysqli_query($conn, $sql); while($row = mysqli_fetch_array($result)) // get all the data from the database you specified, and puts it an array { $photo=$row["image"]; echo'<div class="container">'; echo'<img src='.'"'.$photo.'"'.'class="image">'; echo'<div class="overlay">'; echo'<div class="text"><button onclick="cartFunction()" id="cartButton" >add to cart </button></div>'; echo'</div>'; echo '</div>'; } echo '<script>'; echo 'var'." ".'button=document.getElementById("cartButton");'; echo 'function'." ".'cartFunction(){'; echo 'var'." ".'sum=0;'; echo 'var'." ".'price='. $row["Price"].';'; echo 'price+=sum;'; echo 'price'; // meant to print price echo '}'; echo '</script>'; ?> </body>

12th Apr 2018, 11:22 AM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar
2 Answers
+ 6
Use chrome tool to check what your source code looks like.
12th Apr 2018, 12:34 PM
Toni Isotalo
Toni Isotalo - avatar
0
i checked the source code in the chrome web browser and this is the code that showed. <script> var button=document.getElementById("cartButton"); function cartFunction(){ var sum=0; var price=; price+=sum; price //it seems like it is printing out the word print instead of the value } //i tried echo 'document.write(price);'; but that doesnt seem to work either, could you help me out while i try to find another solution on google </script>
12th Apr 2018, 1:05 PM
Mogammad Shameer Losper
Mogammad Shameer Losper - avatar