both of those are examples of odd hands that you were in for many reason - not just the flush. In those instances it's hard not to play them out. You opponents made value bets and you called them down.
Those hands are just poker. My answer to your question though is not to play for the flush with what I call jack-on hands (jxs) - this is as opposed to jackoff hands (jxo).
But these hands are just poker. You played them fine.