How come there is gravity on earth but not is space?
You are incorrect in your assumption. There is gravity everywhere throughout the universe. The planets and moons orbit one another entirely because of gravity. You are making the common mistake of thinking that things are weightless in space for lack of gravity. The reason for weightlessness in space is that an object in orbit is essentially falling, and all of the things in it are falling at the same speed. If you were in an elevator, and someone cut the rope, you and the elevator would be falling at the same speed, and it would seem to you that you were floating in the elevator, until of course the elevator hit bottom and upset the illusion. Rudely, I might add.