I'm new in php, I have made a contact form with Javascript. But when I click submit button the whole page refreshes. Please help me
Asked
Active
Viewed 107 times
-1
-
4Can you show the problematic code as right now it is difficult to help you? – Darin Dimitrov Apr 21 '11 at 07:00
-
2Please read this blog post. It tells you how to ask a good question. We can't help you unless you give us details : http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – JohnP Apr 21 '11 at 07:01
-
1Your question actually has nothing to do with PHP. Javascript is the language you should dive into. – kapa Apr 21 '11 at 07:38
-
1tag "php" also for such questions !! – Rikesh Apr 21 '11 at 08:08
-
@RIKY I removed it because this question has nothing to do with it. – kapa Apr 21 '11 at 08:51
2 Answers
2
When you click a submit button, the expected behaviour from the browser is that it submits the form. If you want to prevent this default action from happening, read about event.preventDefault()
or returning false
from an event handler.
I give you an example. HTML:
<form id="my-form" action="">
...
<input type="submit" value="Submit me please" />
</form>
Javascript:
var myform=document.getElementById('my-form'); //getting our form
myform.onsubmit=function (e) { //attaching a handler to the submit event
if (e && e.preventDefault) { //modern way of preventing default action
e.preventDefault();
}
else if (window.event && window.event.returnValue) { //ancient IE crap
window.event.returnValue = false;
}
};
Next time try to be more specific.

kapa
- 77,694
- 21
- 158
- 175
-
If you bind an event handler this way, you can simply `return false`. Moreover, always give the form a default `action`, so a user can still submit its contents if JavaScript is disabled/unavailable. – Marcel Korpel Apr 21 '11 at 07:40
-
@Marcel Korpel You are right with your first statement, but I mentioned this in my answer, I gave just one example though. I don't understand your 2nd statement, my form has a default action. – kapa Apr 21 '11 at 07:45
-
Yes, I see you mentioned it in your answer, but consistency is my friend. ;-) And with default action I mean you should fill the `action` attribute with a URL, so a user can still submit the form without JavaScript being enabled. – Marcel Korpel Apr 21 '11 at 07:47
-
@Marcel An empty `action` attribute means the form is submitted to the current URL. I might be wrong, but for the last few years it worked flawlessly for me. – kapa Apr 21 '11 at 07:48
-
Wow, I didn't know that. On the other hand, [the spec](http://www.w3.org/TR/html401/interact/forms.html#edef-FORM) says: “User agent behavior for a value other than an HTTP URI is undefined.” – Marcel Korpel Apr 21 '11 at 07:58
-
Still, behaviour is consistent among browsers. For HTML5, now it is a part of the spec (like many other things that were cross-browser but were standardized in HTML5). [See this thread](http://stackoverflow.com/questions/1131781/blank-html-form-action-posting-back-to-self). – kapa Apr 21 '11 at 08:13
0
Submit should refesh (Post) the page because you have to send/post the data to the server unless you are going yo use ajax and not use submit button to handle it but refreshing the page after clicking submit button is normal.

Ahmed Magdy
- 5,956
- 8
- 43
- 75