views:

324

answers:

9

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

A: 

Preferrably without ajax or jquery...

That is not possible as far as I know :( The way to go is with ajax (or iframe in your case as suggested by others), here are more details about it:

See this please:

http://www.simonerodriguez.com/ajax-form-submit-example/

and here is a good tutorial to get you started yourself:

http://www.yourhtmlsource.com/javascript/ajax.html http://www.w3schools.com/ajax/default.asp

You can do that easily with jquery too, here is the prototype:

$.ajax({
 url:'url',
 method:'post'
 data:data,
 success:function(response){
  alert(response);
 }
});
Sarfraz
A: 

Here is some jQuery for posting to a php page and getting html back:

$('form').submit(function() {
    $.post('tip.php', function(html) {
       // do what you need in your success callback
    }
    return false;
});
Keith Rousseau
Care to explain why you downvoted? You can't do it without Ajax
Keith Rousseau
@Keith No downvote from me, but maybe it was because you mentioned jQuery and he said no jQuery.
alex
@Keith - on the contrary, the iframe method suggested by several answers can be used.
justkt
Those are the wrong arguments for the post method, you aren't collecting any data from the form, and you use a generic "applies to all forms" selector with a "posts to a specific uri instead of getting the action from the form" post call.
David Dorward
@David - it's a basic example, that is a valid post call and there is only 1 form on the page.
Keith Rousseau
It isn't. The second argument should not be the callback.
David Dorward
Oh, good grief. jQuery does type checking so arguments can be skipped. Yuck.
David Dorward
Yes, they do that all over the place. Welcome to the jQuery API
Keith Rousseau
A: 

The page will get reloaded if you don't want to use javascript

baloo
...or do something ugly like a `<iframe name="ew" />` and `<form target="ew" />`
alex
A: 

Why better without ajax or jquery? Jquery is very powerful and user-friendly and it is actually easy to learn, if you managed to learn php, jquery will be just fun.
Check this: http://api.jquery.com/jQuery.post/ , but before that, learn jquery basics: http://jquery.com/
Believe me, your visitors will like jquery!

hey
+2  A: 

You either use AJAX or you

  • create and append an iframe to the document
  • set the iframes name to 'foo'
  • set the forms target to 'foo'
  • submit
  • have the forms action render javascript with 'parent.notify(...)' to give feedback
  • optionally you can remove the iframe
Sean Kinsey
That's still Ajax. Not XHR, but still Ajax.
David Dorward
Not really (even though I can see what your'e getting at) - Ajax is primarily a term that evolved around the use of the XMLHttpRequest object but which in later years has come to mean 'anything cool'. But this still doesn't change what it is. Either way, used in a sentence like 'without jQuery or Ajax' pretty much explains the OP's knowledge about.. well, anything..
Sean Kinsey
+1  A: 

You can try setting the target attribute of your form to a hidden iframe, so the page containing the form won't get reloaded.

I tried it with file uploads (which we know can't be done via AJAX), and it worked beautifully.

wtaniguchi
A: 

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.

alex
A: 

You'll need to submit an ajax request to send the email without reloading the page. Take a look at http://api.jquery.com/jQuery.ajax/

and your code should be something along the lines of:

$('#submit').click(function(){
    $.ajax({
        url: send_email.php,
        type:'POST',
        data: {
            email: email_address,
            message: message
        },
        success: function(msg){
                alert("Email Sent');
            }                   
        });
    }
});

The form will submit in the background to the send_email.php page which will need to handle the request and send the email.

jkilbride
A: 

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.

DanC