Sayyiduna Sa'd (may Allah be pleased with him) narrates that the Messenger of Allah (peace and blessings be upon him) said: "When one of you spits in the mosque, he should bury it (in the soil), so that it does not touch the body or the clothes of a believer and cause him harm."
Related hadith on this topic