Is there a way to display live calculation with an information taken from mysql database? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Is there a way to display live calculation with an information taken from mysql database?

Hey there i am developing a web application and i need to display a live calculation as soon as the fields are filled with information and what do i mean by a live calculation is something as live as a ng-model in a ng-app, the problem goes like this... The user choses a product from a dropdown list which list's choices are coming from a mysql database and the price of the chosen product should be the first multiplyer while the second myltiplyer is the next input field where the custommer should enter how many pieces they want. I tried to take the price from the table where product is selected but i believe that php needs a submission in order to proccess the data and actually see which product has been selected, so is there any way to display a live calculation and inform the customer how much ther order will cost before they submit the form?

2nd Jan 2019, 9:08 AM
Miki Sarkovski
Miki Sarkovski - avatar
5 Answers
+ 3
Hi! if i understood you question, what you want is to be able to pull the price from the db and calculate it with the user input without reloading/refreshing (submitting the data) page? of so then you nees to use AJAX to submit/retrieve the prices et calculate the total, the page will not refresh and the calculation should be live!
5th Jan 2019, 12:20 AM
StoneCoder🇬🇦
StoneCoder🇬🇦 - avatar
+ 2
im also new to Ajax but i think i can provide some help. im going to work on it and will get back to you!
5th Jan 2019, 2:13 PM
StoneCoder🇬🇦
StoneCoder🇬🇦 - avatar
+ 2
Hi! sorry it took long here is the html/JavaScript part. https://code.sololearn.com/WWhJ0D8Tlo4x/#html PHP store the code in a file called product.php on the same folder as index.php https://code.sololearn.com/wRwBit9Nb8bV/#php I have tried to comment/explain everything in js, i supposed to you familiar with PHP/SQL.
6th Jan 2019, 1:41 PM
StoneCoder🇬🇦
StoneCoder🇬🇦 - avatar
+ 1
Yes, your assumption is right and your suggestion is probably the best and maybe the only way to solve my problem but the thing is that i am 18 days old in this world and i learn as i go, so ajax is something completely new for me since this is my first interaction with it and from all the researches that i have made so far nothing seems to offer a proper solution for this specific problem that i have so my best guess is that i will have to learn and understand the logic behind ajax completely and then use it to find a proper solution to my problem (which seems really hard from my point of view right now) so if you have an extra minute in your life and you really love to practice your problem solving skill check the link below for further information and how far i have gotten with my problem (without using ajax) and help me learn more about ajax by actually giving me a solution along with an explanation of how ajax retrieves data and makes the calculation happen. https://stackoverflow.com/q/54005578/8007596
5th Jan 2019, 8:26 AM
Miki Sarkovski
Miki Sarkovski - avatar
0
Hey Jon, thank you for your time invested and effort put in writing this piece of code. I have finally made some time to check it and for some odd reason the code is not working. I have stored the code in two different files just like you mentioned but the "Total" field remains empty no matter what, then i remembered to try and put the entire code inside the index.php which resulted with few notes and an error that look like this http://prntscr.com/m5y2gy ... I tried to fix it on my own but as i said before, I am completely unfamiliar with Ajax and i failed to solve the ongoing problem multiple times that led me to asking the code creator (which is you) for some further assistance. I will be waiting for your response, thanks again!
11th Jan 2019, 5:51 PM
Miki Sarkovski
Miki Sarkovski - avatar