I have a classifieds website, and on the page where ads are showed, I am creating a "Send a tip to a friend" form...
So anybody who wants can send a tip of the ad to some friends email-adress.
I am guessing the form must be submitted to a php page right?
<form name='tip' method='post' action='tip.php'>
Tip somebody: <input name="tip_email" type="text" size="30" onfocus="tip_div(1);" onblur="tip_div(2);"/>
<input type="submit" value="Skicka Tips"/>
<input type="hidden" name="ad_id" />
</form>
When submitting the form, the page gets reloaded... I don't want that...
Is there any way to make it not reload and still send the mail? Preferrably without ajax or jquery...
Thanks
I tried many times for a good solution and answer by @taufique helped me to arrive at this answer.
NB : Don't forget to put
event.preventDefault();
at the beginning of the body of the function .Fastest and easiest way is to use an iframe. Put a frame at the bottom of your page.
<iframe name="frame"></iframe>
And in your form do this.
and to make the frame invisible in your css.
Have you tried using an iFrame? No ajax, and the original page will not load.
You can display the submit form as a separate page inside the iframe, and when it gets submitted the outer/container page will not reload. This solution will not make use of any kind of ajax.
You will need to use JavaScript without resulting to an
iframe
(ugly approach).You can do it in JavaScript; using jQuery will make it painless.
I suggest you check out AJAX and Posting.
You either use AJAX or you
It's a must to take help of
jquery-ajax
in this case. Withoutajax
, there is currently no solution.First, call a JavaScript function when the form is submitted. Just set
onsubmit="func()"
. Even if the function is called, the default action of the submission would be performed. If it is performed there would be no way of stoping the page from refreshing or redirecting. So, next task is to prevent the default action. Insert the following line at the start offunc()
.Now, there will be no redirecting or refreshing. So, you simply make an ajax call from
func()
and do whatever you want to do when call ends.Example:
Form:
Javascript: