I commented on another similar post. If you have diarrhea while fasting, you are releasing electrolytes which means you are taking in too many to process. Over doing something, maybe magnesium or salt?
From my research into this specific symptom disconnected from fasting I’ve learned that this is usually due to your pancreas not being able to process all the sugars currently in your system so they reach the colon where the bacteria create the foam I think. Foam in pee is usually due to kidney issues and is caused by protein in the pee. Hope this helps