aniskazi:
I am using the arduino mega2560
Then why not use a hardware serial port?
aniskazi:
I am using the 3.3V pin of the arduino to connect the esp.
Don't do that, the 3.3V pin of the Arduino can only supply 50mA, the ESP can draw up to 5 times as much.
Also, you need a bypassing capacitor: when transmitting WiFi, it draws a lot of current in very small peaks, without bypass caps, your voltage will drop low enough to make it unstable.
aniskazi:
And the esp is still working fine.
Not for long if you are connecting its 3.3V input pins to 5V outputs of the Mega. You will destroy the ESP.
Use a voltage divider or a level shifter.
First make sure your hardware is correct, once that is done, we can start to look at what's wrong with your code.
I explained how to wire it in this post: ESP8266 - #9 by PieterP - Project Guidance - Arduino Forum. (forget about the LEDs and the program button, you don't need them.)