send a form with serialize() function and extra data in ajax()

I'm programming a web page for a company in CI3, and I've another problem. I need to send to wizard.php(controller)a serialized form and a javascript variable, but, I think that I can't send 2 variables at the same time, here is my code.

var idCompany =5;
$(document).on('submit', '#period-form', function(event) {
    event.preventDefault();
    $.ajax({
        url: '<?php echo base_url()?>wizard/insertPeriod/'
        type: 'POST',
        dataType: 'json',
        data: $("#period-form").serialize(),
        success : function (json) {
            $("#alert").append(json.response_alert);
        },
        error : function (xhre) {
            console.log(xhre);
        }
    })
});

As you can see, idCompany is an attribute, it has value but, I put value in another function. is possible add values to a serialized form? or how I can send a serialized form and a variable at the same time?

Upvotes: 1

Views: 4918

Answers (2)

Koen Peters
Koen Peters

Reputation: 12916

The serialize function simply creates an URL query string from all the form elements. So you can manually add the variable to the result of the serialize() function like this:

 data: $("#period-form").serialize() + '&idCompany=' + idCompany

Upvotes: 3

rahul pasricha
rahul pasricha

Reputation: 931

var data = $('#period-form').serializeArray();
data.push({ name: "<somename>", value: "<somevalue>" });

You can use push method to add more name - value pairs.

Upvotes: 1

Related Questions