user1894647
user1894647

Reputation: 663

Passing textbox array to ajax json

I have two different arrays namely main_name[] and sub_name[]. I have to pass these values to the next page using AJAX, for inserting into DB.Could you please help me out, how could I pass the array as data in AJAX. HTML

<input type="text" name = "main_name[]" value = "" >
<input type="text" name = "sub_name[]" value = "" >

JS

$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});

Upvotes: 3

Views: 1255

Answers (3)

Deepak Ingole
Deepak Ingole

Reputation: 15772

You could simply use serializeArray

var data = $(form).serializeArray();

$.ajax({
    type: "POST",
    dataType: "json",
    url: "response.php", //Relative or absolute path to response.php file
    data: data,
    success: function (data) {
        $(".the-return").html();
    }
});

Upvotes: 2

Tismon Varghese
Tismon Varghese

Reputation: 869

$('form').serialize() will serialize all your form elements together with values and send along with an ajax query.

 $.ajax({
     type: "POST",
     dataType: "json",
     url: "response.php", //Relative or absolute path to response.php file
     data: $('#form_id').serialize(),
     success: function(data) {
       $(".the-return").html
     );
     alert("Form submitted successfully.\nReturned json: " + data["json"]);
    }
    });
    return false;
    });
    });

Upvotes: 2

Syed mohamed aladeen
Syed mohamed aladeen

Reputation: 6755

try this it will get all values of your form in (data) variable and send through ajax in json format.

var data = $('form').serialize();
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});

Upvotes: 1

Related Questions