0

Hi all I have a AJAX query which I am calling using an onclick function as seen below, the query successfully POSTs the data from the form I have on my page - and I can use the data on my php script without an issue:

function tempFunction(obj){
        var no= $(obj).attr('id');
        $.ajax({
           type: "POST",
           url: "/tempproject/main/changepage",
           data: $('form').serialize(),
           success: function(msg){
                alert( "success: " + msg ); //Anything you want
           }
        });
        window.alert(no);
    }

However I wish to also send the variable no along with the form data, I am a complete newbie when it comes to JS so could someone point me in the right direction of how to send the variable along with the serialized form data? can I append the serial data somehow? I feel this is probably really easy but I'm to new to JS

TotalNewbie
  • 1,004
  • 5
  • 13
  • 25
  • hope stackoverflow has an answer ready [link](http://stackoverflow.com/questions/6627936/jquery-post-with-serialize-and-extra-data) – Lepanto Apr 02 '14 at 10:09

2 Answers2

3

try this

function tempFunction(obj) {
    var data = $('form').serializeArray();
    data.push(
        {
            no: $(obj).attr('id')
        }
    );

    $.ajax({
        type: "POST",
        url: "/tempproject/main/changepage",
        data: data,
        success: function (msg) {
            alert("success: " + msg); //Anything you want
        }
    });
    window.alert(no);
}
MaiKaY
  • 4,422
  • 20
  • 28
0

You may try to use jQuery Form plugin. It allows ajax submitting form transparently, without worrying about serizalization.

Plugin: http://malsup.com/jquery/form/

Your code:

$('#form').ajaxSubmit({
 data: { no: $(obj).attr('id') },
 url: "/tempproject/main/changepage",
 success: function(msg){
  alert( "success: " + msg ); //Anything you want
 }
});
Artek Wisniewski
  • 797
  • 5
  • 13