1

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?

Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
  • There are some ideas on how to do this below, but another option would also be to inclue your extra variable in a hidden input inside the form – Seth McClaine May 08 '15 at 22:36
  • Possible duplicate of [How to add data via $.ajax ( serialize() + extra data ) like this](http://stackoverflow.com/questions/4406348/how-to-add-data-via-ajax-serialize-extra-data-like-this) – Sagar Naliyapara Nov 02 '15 at 11:53

2 Answers2

3

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
Koen Peters
  • 12,798
  • 6
  • 36
  • 59
1
var data = $('#period-form').serializeArray();
data.push({ name: "<somename>", value: "<somevalue>" });

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

rahul pasricha
  • 931
  • 1
  • 14
  • 35