My Iphone will only work on charger and won't let me turn on Wi-Fi?

Yesterday my iPhone randomly turned off and wouldn't turn back on. Then it finally turned on when I put it on the charger. So when it's on the charger it kind of turns off and back on here and there. But right when I take it off the charger it turns right off. And it will not let me connect to Wi-Fi. Like I'll bring up the Wi-Fi screen and it won't let me switch it from off to on.

Sometimes i do have this problem. As i have iphone 3gs it has low processor, ram and it doesn't suites for ios6.0 or above, in that case it get strucks and turns off. I fixed it by closing all multi tasking apps and taking a backup in itunes. Try this it may work for you also.