Ajax to PHP on the same page

后端 未结 2 1904
深忆病人
深忆病人 2020-12-11 06:50

I’m trying to send variables to the same page that makes the AJAX call.

I receive successful result only if I separate the PHP script (e.g. process.php

2条回答
  •  悲&欢浪女
    2020-12-11 06:59

    add die(); function AND use $_SERVER['HTTP_X_REQUESTED_WITH'] which detect ajax request. alos put serialize() out side of ajax function to get data

       var data = '';
       $(function() { 
        $('form').submit(function(e) { 
            e.preventDefault();
            data = $(this).serialize();
            $.ajax({
                type        : 'POST',
                url         : 'index.php',
                data        :  data,
                dataType    : 'json',
                encode      : true
            })
            .done(function(data) {
                $('#result').html(data);    
            })
        });
    }); 
    
    
    
    

提交回复
热议问题