Form submit with AJAX passing form data to PHP without page refresh

后端 未结 10 1476
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:07

Can anyone tell me why this bit of code isn\'t working?


  
    

        
10条回答
  •  -上瘾入骨i
    2020-11-22 06:33

    $(document).ready(function(){
    $('#userForm').on('submit', function(e){
            e.preventDefault();
    //I had an issue that the forms were submitted in geometrical progression after the next submit. 
    // This solved the problem.
            e.stopImmediatePropagation();
        // show that something is loading
        $('#response').html("Loading data...");
    
        // Call ajax for pass data to other place
        $.ajax({
        type: 'POST',
        url: 'somephpfile.php',
        data: $(this).serialize() // getting filed value in serialize form
        })
        .done(function(data){ // if getting done then call.
    
        // show the response
        $('#response').html(data);
    
        })
        .fail(function() { // if fail then getting message
    
        // just in case posting your form failed
        alert( "Posting failed." );
    
        });
    
        // to prevent refreshing the whole page page
        return false;
    
        });
    

提交回复
热议问题