Close correlation between magnetic properties and the soft phonon mode of the structural transition in BaFe2As2 and SrFe2As2