How to send a form without refreshing the page?

后端 未结 4 1157
梦毁少年i
梦毁少年i 2021-01-16 15:04

I don\'t know PHP.

I don\'t want the user to go to http://www.example.com/feedback-thanks.html after sending the form. What I want is text below the sen

相关标签:
4条回答
  • 2021-01-16 15:14

    You will have to use $.ajax() for that

    • http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/
    • http://www.sitepoint.com/ajax-jquery/

    this is what I use in one of my apps

    $('#submitSearch').click(function() {
    
            var formData = {
                searchData: $('#searchData').val(),
            };
    
            $.ajax({
                url: siteUrl + 'fetch/search',
                type: "POST",
                data: formData,
                cache: true,            
                beforeSend:function(){
                    jQuery('#main').html('<div class="loading"><img src="' + siteUrl + 'resources/imgs/ajax-loader.gif" alt="Loading..." /></div>');
                },
                success: function(data) {
                    jQuery('#main').empty();
                    jQuery('#main').append(data);
                },
                error:function(x,e){
                    if(x.status==0){
                        alert('You are offline!!\n Please Check Your Network.');
                    }else if(x.status==404){
                        alert('Requested URL not found.');
                    }else if(x.status==500){
                        alert('Internel Server Error.');
                    }else if(e=='parsererror'){
                        alert('Error.\nParsing JSON Request failed.');
                    }else if(e=='timeout'){
                        alert('Request Time out.');
                    }else {
                        alert('Unknow Error.\n'+x.responseText);
                    }
                }
            });
            return false;
        });
    
    0 讨论(0)
  • 2021-01-16 15:18
    1. posibility - Use ajax in any form - jQuery, plain, etc ... see Google

    2. posibility - Create hidden iframe with name="sender" and use form target attribute ( I'm not sure if it is valid, but it works without javascript )

    0 讨论(0)
  • 2021-01-16 15:19

    If you want what i want just look at this tutorial (with demo)

    http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

    0 讨论(0)
  • 2021-01-16 15:37

    I'd suggest using a Javascript framework like jQuery and use it's AJAX function. Lots of tutorials to find on the internet if you Google for it.

    Start at jquery.com

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