du67560 2016-01-01 00:53 采纳率: 0%
浏览 35
已采纳

if / else在php脚本中不能处理ajax请求


I'm working on a website with heavy Ajax, and I'm using codeigniter for building it.
I've got a form with post method, and this form is being posted using ajax request which call a function with if/else statement, like this:

    if(isset($_POST['save']))
    {
        $data['phase'] = 'translating';
    }
    elseif(isset($_POST['submit']))
    {
        $data['phase'] = 'waiting_approve';
    }

The if/else statement check which button is being clicked, and it works 100% after posting the data of the form in the usual way, but never works when posting it using ajax request.
My ajax request:

    $('#workspace-content').delegate('form#article', 'submit', function(){
    var that = $('form#article'),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

        data = that.serialize();

        $.ajax({
            type: type,
            url : url,
            data : data,
            dataType: 'json',

            success: function(data){
                $('#header-search-field').append(data.msg).delay(3000).fadeOut(500, function(){
                    var that = $(this);
                    that.html('').fadeIn();
                });
            }
        });

    return false;
});

Any suggestion or solution?!!


The HTML form:

<button id="save-article" type="submit" name="save" class="btn btn-info btn-xs pull-left">
    <span class="glyphicon glyphicon-floppy-save"></span>
</button>
<input name="title" type="text" value="<?php echo $work->title;?>" class="form-control" id="title" placeholder="" />
<textarea row="10" name="article" class="form-control" id="article" placeholder=""><?php echo $work->article;?></textarea>
<button id="submit-article" type="submit" name="submit" class="btn btn-info btn-block">Send</button>
<input name="slug" type="hidden" value="<?php echo $work->slug;?>" />
  • 写回答

1条回答 默认 最新

  • douzhang2092 2016-01-01 01:35
    关注

    When you submit a form normally, the button that you used to submit it will be included in the POST data. But when you submit with AJAX, and use that.serialize(), the POST data just includes the input fields -- the submit button isn't included. You need to attach your submit code to the buttons, so you can add the appropriate values to the data.

    $('#workspace-content').on('click', 'form#article .btn', function(){
        var that = $(this).closest("form"),
            url = that.attr('action'),
            type = that.attr('method'),
            data = that.serialize();
        data += '&' + this.name + '=1';
    
        $.ajax({
            type: type,
            url : url,
            data : data,
            dataType: 'json',
    
            success: function(data){
                $('#header-search-field').append(data.msg).delay(3000).fadeOut(500, function(){
                    var that = $(this);
                    that.html('').fadeIn();
                });
            }
        });
    
        return false;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探