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 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制