Search code examples
codeigniterjquery-ui-sortableajaxform

ajaxform() & sortable('serialize'), and codeigniter .. submitting form fields and list elements


i have a form and a sortable() list where a user can drag from a pre populated list, into an empty UL. The user would drag the LIs that they want into to this empty UL.. There is also a form with a few text boxes that the user fills out and clicks submit.

I can get the ajaxform to submit the form data and display it in a flashdata session, but i cant get it to show the allowed_fields data. (which is the sortable list). I know its serializing it because running alert(serializedList); returns the serialized list of elements.

This is the JS to generate the sortable lists:

/**
 * sortable ul items
 * 
 * this is used for the add levels page to associate allowed_fields 
 * to a level.
 */
$('.block-list').sortable({
    connectWith: '.block-list',
    placeholder: 'placeholder'
});

This is the JS to process the ajaxSubmit:

/**
 * showResponse(data)
 * show the response if the form submission is successful
 * @param  {object} data object of message or success
 * @return {null}
 */
function showResponse(data){
    alert(serializedList);
    if (data.errorStatus == 1){
        $.jGrowl(data.message, { theme: 'error' });
    }else{

        $.jGrowl(data.message, { theme: 'success' });
    }
}//end showResponse()

/** @type {Object} setup the options for the ajax submit forms. */
var submitOptions = {
    success: showResponse,
    beforeSubmit: function(){ serializedList = $("#allowed-fields-list").sortable('serialize');  },
    dataType: 'json',
    resetForm: true ,
    data: { allowed_fields: serializedList }
};      
$("#addlevel-form").ajaxForm(submitOptions);

and this is the code igniter function that will handle the form data..

public function addlevelprocess(){
    $message = array(
        'message' => 'Successfully Added The Level To The Database! WHOA!:'.$this->input->post(),
        'errorStatus' => 0
    );
    $this->session->set_flashdata('post', $this->input->post());
    echo json_encode($message);
}

how can i get ajaxform to send both form field data and the sortables() data.


Solution

  • i ended up using ajaxSubmit() and passing the serialized form information via data:

    $('#addlevel-form').submit(function() {
        /** @type {serialized} serialized array of field IDs for allowed fields */
        var allowedFields = $('#allowed-fields-list').sortable('serialize');
        var formInfo = $('#addlevel-form').formSerialize();
        /** @type {Object} setup the options for the ajax submit forms. */
        var submitOptions = {
            url: '/admin/levels/addlevelprocess.html',
            dataType: 'json',
            success: showResponse,
            data: { allowed_fields: allowedFields, levelInfo: formInfo }
        };      
        $(this).ajaxSubmit(submitOptions);
        return false;
    });