0

Hi guys so basically i am trying to add data in a nutrtion label which i built. So the label it self has everything from fat, carbs , protein etc. Now i have set up a database with:

ingName: ...
fat: ...
Carbs: ....
etc etc 

So right now i am just trying to get it to work for fat. Once i do this i can easily do the others. I can search through the database easily.When the user presses Add button it will add the ingredient below the search box and then change the content of fat. However if the user adds anther record the fat content wont add up together.

So for example i have Banana which has 4 fat and apple which has 1 fat. I add apple first and the fat content changes to 1 fat. When i add banana it should become 5 fat, however it will actually go to 4. So the records are not being added up correctly.

Js:

        else{ 
            var searchedValue = $('#search_term').val(); 
            var temp2 = 0; 
            $.post( 'build.php', { 'search_term':searchedValue, 'current_fat':temp2 }, function(data) { 
            $('.result').html( data ); 
            }); 
            temp = $("#fat").text(); 
            temp = parseInt(temp); 
            current_fat += temp2; 
            current_fat += temp; 
            console.log(temp + " " + temp2 + " " + current_fat); 
            $("#fat").text(current_fat);

The part which is causing the problem is in the else loop, i cant seem to figure out how to fix it,

If you need the php file i can get you that, but it seems to be in the else loop in the js file but i cant seem to figure out how to fix it. Been about a week i have been stuck on this mental problem :)

Thanks anyway for the help x

10
  • 1
    It looks like every time you're entering the else block you're setting temp2 = 0, and always adding 0 to current_fat. so you're only going to get whatever the value was, plus 0. in your banana example, it would be 4. Commented Mar 21, 2016 at 13:41
  • hmm this is a good point, but i tried alternatives to this and just did not work , so if you have anything let me know :) cause i am fresh out of ideas Commented Mar 21, 2016 at 13:42
  • Where do you update the value of temp2? I can't seem to find it. Commented Mar 21, 2016 at 13:43
  • I'm not sure why you need temp2 at all. it only is ever equal to 0. Commented Mar 21, 2016 at 13:46
  • I actually didn't , i used it as just a temp thing to try and find out what is going on with this crazy program , i dunno if the php will help u with this probkem if u want to see that Commented Mar 21, 2016 at 13:46

1 Answer 1

1

Try this:

$('#addButton').on('click', function( event ) {
   //your other stuff
   var searchedValue = $('#search_term').val(); 
   //remove this : var temp2 = 0; 
   //add this:
   temp = $("#fat").text(); 
   temp = parseInt(temp);
   //change your post reg to:
   $.post( 'build.php', { 'search_term':searchedValue, 'current_fat':temp }, function(data) { 
       $('.result').html( data ); 
    }); 
   //rest of your code...
Sign up to request clarification or add additional context in comments.

5 Comments

OMG, u are a life savior, 2 weeks been stuck on this, however the only problem is now, my boxes dont display below the search box , so the adding bit is fine now :0 which i am so happy about, but now the boxes when i press add dont display below
glad I could help. if this fixed your adding issue, please remember to accept the answer. you may need to do a new question for the not displaying part
hmm i mean its the var divHolder = $('.selectedStuff'); which is not displaying right now , would you not know why?
hrm...idk, make sure you added all the code below that back in after my answer.
Fixed it dont worry now :0 its the remove(); fucntion which is not working, so when the user clicks on the red arrow to remove it, that does not work but i think i need to ask in anther question i guess x

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.