AJAX - Multiple Data

I'm stuck: I'm trying to submit a form using AJAX, but cannot find a way to submit multiple data fields through 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 tried all kinds of things:

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

Or even things like this for testing:

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

But no matter what I tried, I got nothing in my activity_save.php , nothing in my SQL.

So what is the correct syntax for placing more lines in my AJAX call?

+122
jquery sql ajax
May 22 '11 at 2:25
source share
13 answers

The correct syntax is: http://api.jquery.com/jQuery.ajax/

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

If this does not work, I would warn these variables to make sure they matter.

+229
May 22 '11 at 2:30 a.m.
source share

you can send data via Json or a regular post, here is an example for Json.

  var value1 = 1; var value2 = 2; var value3 = 3; $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "yoururlhere", data: "{'data1':'" + value1+ "', 'data2':'" + value2+ "', 'data3':'" + value3+ "'}", success: function (result) { //do somthing here } }); 

If you want to use it through a regular post, try this

  $.ajax({ type: "POST", url: $('form').attr("action"), data: $('#form0').serialize(), success: function (result) { // do somthing here } }); 
+32
May 22 '11 at 2:39 a.m.
source share

Try using quotation marks:

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

It should work fine.

+9
Mar 19 '14 at 13:10
source share
 var countries = new Array(); countries[0] = 'ga'; countries[1] = 'cd'; 

after that you can do:

 var new_countries = countries.join(',') 

after

 $.ajax({ type: "POST", url: "Concessions.aspx/GetConcessions", data: new_countries, ... 

This thing works like a JSON string format.

+4
May 22 '11 at 5:36
source share

This works for me.

Here is my php:

 <div id="pageContent"> <?php while($row = mysqli_fetch_assoc($stmt)) { ?> <br/> <input id="vendorName_" name="vendorName_<?php echo $row["id"]; ?>" value='<?php echo $row["vendorName"]; ?>'> <input id="owner_" name="owner_<?php echo $row["id"]; ?>" value='<?php echo $row["owner"]; ?>'> <input id="city_" name="city_<?php echo $row["id"]; ?>" value='<?php echo $row["city"]; ?>'> <button id="btn_update_<?php echo $row["id"]; ?>">Update</button> <button id="btn_delete_<?php echo $row["id"]; ?>">Delete</button> <?php } ?> </br></br> <input id = "vendorName_new" value=""> <input id = "owner_new" value=""> <input id = "city_new" value=""> <button id = "addNewVendor" type="submit">+ New Vendor</button> </div> 

Here is my jQuery using AJAX:

 $("#addNewVendor").click(function() { alert(); $.ajax({ type: "POST", url: "create.php", data: {vendorName: $("#vendorName_new").val(), owner: $("#owner_new").val(), city: $("#city_new").val()}, success: function(){ $(this).hide(); $('div.success').fadeIn(); showUsers() } }); }); 
+3
May 09 '14 at 3:29
source share

According to http://api.jquery.com/jquery.ajax/

 $.ajax({ method: "POST", url: "some.php", data: { name: "John", location: "Boston" } }) .done(function( msg ) { alert( "Data Saved: " + msg ); }); 
+3
May 18 '15 at 18:28
source share

I am new to ajax, but I am thinking of using this method "data: {status: status, name: name}" the data type should be set to JSON i.e.

 $.ajax({ type: "POST", dataType: "json", url: "ajax/activity_save.php", data: {status: status, name: name}, 
+2
Sep 26 '13 at 16:11
source share

I am new to AJAX and I have tried this and it works well.

 function q1mrks(country,m) { // alert("hellow"); if (country.length==0) { //alert("hellow"); document.getElementById("q1mrks").innerHTML=""; return; } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("q1mrks").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","../location/cal_marks.php?q1mrks="+country+"&marks="+m,true); //mygetrequest.open("GET", "basicform.php?name="+namevalue+"&age="+agevalue, true) xmlhttp.send(); } 
+1
Apr 29 '13 at 6:20
source share

Use this

 data: '{"username":"' + username + '"}', 

I try to use the syntax to work with laravel; it works for me for laravel 4.2 + ajax.

+1
Jul 26 '14 at 15:18
source share

Try the following:

 $(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) {... 
+1
Oct 26 '14 at 22:27
source share

Try using:

  $.ajax({ type: "GET", url: "something.php", data: { "b": data1, "c": data2 }, dataType: "html", beforeSend: function(){ }, error: function(){ alert("Error"); }, success: function(data){ $("#result").empty(); $("#result").append(data); } }); }); 
+1
Jun 14 '17 at 16:03
source share

just add "& user" or using json serialization to turn it into json

0
Feb 06 '14 at 9:14
source share

Here is what works for me after 2 days of scratching my head; why I could not get the AJaX β€œdata” parameter to send two keys / values ​​(including a variable containing raw image data) was a mystery, but it looks like the jQuery.param () function was written for:

create an array of params with your variables without quotes:

 var params = { key_name1: var_1, key_name2: var_2 }; // etc. var ser_data = jQuery.param( params ); // arbitrary variable name 

Use the ser_data variable as the value of your information;

  $.ajax({ type: 'POST', url: '../php_handler_url.php', data: ser_data, }).success(function(response) { alert(response); }); 

The documentation is here: https://api.jquery.com/jQuery.param/

Hope this helps!

0
Jun 22 '16 at 16:03
source share



All Articles