Skip to content
Advertisement

How to send multiple data fields via Ajax? [closed]

I’m stuck: I’m trying to submit a form using AJAX, but I can’t find a way to send multiple data fields via my AJAX call.

$(document).ready(function() {
  $("#btnSubmit").click(function()  {
    var status = $("#activitymessage").val();
    var name = "Ronny";
    $.ajax({
      type: "POST",
      url: "ajax/activity_save.php",
      **data: "status="+status+"name="+name"**,
      success: function(msg) {...

I’ve tried all sorts of stuff:

data: {status: status, name: name},

Or even stuff like this just for testing purposes:

data: "status=testing&name=ronny",

But whatever I try, I get nothing in my activity_save.php thus nothing in my SQL.

So, what’s the correct syntax to put more lines of data in my AJAX call?

Advertisement

Answer

The correct syntax is:

data: {status: status, name: name},

As specified here: http://api.jquery.com/jQuery.ajax/

So if that doesn’t work, I would alert those variables to make sure they have values.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement