0


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;?>" />
12
  • I don't usually work with jQuery, but you could try debugging it with var_dump($_POST); and possibly trying var_dump($_REQUEST); just so you can see what's going on. Commented Jan 1, 2016 at 0:58
  • Can you share your HTML form? Commented Jan 1, 2016 at 0:59
  • What's the data being sent in the request? Seems like that would be relevant... Commented Jan 1, 2016 at 1:00
  • 1
    Make sure that the type is POST not GET. By the way, in CodeIgniter, use &get_instance()->input->post() instead of $_POST. Commented Jan 1, 2016 at 1:14
  • 1
    that.serialize doesn't include the button that the user clicked on to submit the form. Commented Jan 1, 2016 at 1:28

1 Answer 1

1

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;
});
Sign up to request clarification or add additional context in comments.

10 Comments

I tried this also: that.find('[name]').each(function(name, value){ var thisInput = $(this); name = thisInput.attr('name'); value = thisInput.val(); data[name] = value; }); but there's the 2 buttons' names with no values :\ . any other suggestions?
Buttons don't have a value, I took that out of my answer. They just have a name.
data[name] = value won't work, because data is a string, not an array.
@Barmar Buttons can/should have values and it's quite good practice to attach a value to a button, especially if there are multiple buttons with same type and name, but different values.
True, but in his form his buttons don't have values, he's just distinguishing them by name.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.