Submit form without page reloading

后端 未结 17 2458
无人及你
无人及你 2020-11-22 00:45

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

相关标签:
17条回答
  • 2020-11-22 01:29

    if you're submitting to the same page where the form is you could write the form tags with out an action and it will submit, like this

    <form method='post'> <!-- you can see there is no action here-->
    
    0 讨论(0)
  • 2020-11-22 01:30

    I've found what I think is an easier way. If you put an Iframe in the page, you can redirect the exit of the action there and make it show up. You can do nothing, of course. In that case, you can set the iframe display to none.

    <iframe name="votar" style="display:none;"></iframe>
    <form action="tip.php" method="post" target="votar">
        <input type="submit" value="Skicka Tips">
        <input type="hidden" name="ad_id" value="2">            
    </form>
    
    0 讨论(0)
  • 2020-11-22 01:30

    It's a must to take help of jquery-ajax in this case. Without ajax, 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 of func().

    event.preventDefault()
    

    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:

    <form id="form-id" onsubmit="func()">
        <input id="input-id" type="text">
    </form>
    

    Javascript:

    function func(){
        event.preventDefault();
        var newValue = $('#input-field-id').val();
        $.ajax({
            type: 'POST',
            url: '...',
            data: {...},
            datatype: 'JSON',
            success: function(data){...},
            error: function(){...},
        });
    }
    
    0 讨论(0)
  • 2020-11-22 01:33

    A further possibility is to make a direct javascript link to your function:

    <form action="javascript:your_function();" method="post">
    

    ...

    0 讨论(0)
  • 2020-11-22 01:34

    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/

    Your code should be something along the lines of:

    $('#submit').click(function() {
        $.ajax({
            url: 'send_email.php',
            type: 'POST',
            data: {
                email: 'email@example.com',
                message: 'hello world!'
            },
            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.

    0 讨论(0)
提交回复
热议问题