1
$.ajax({
type:"post",
data:"name="+name+"&article="+article",

what is the two name mean in the part of data?

Naveed
  • 41,517
  • 32
  • 98
  • 131
cj333
  • 2,547
  • 20
  • 67
  • 110

3 Answers3

5

Read jQuery.ajax()

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting

For example: You are posting name and location to a PHP script to store in database like this.

$.ajax({
   type: "POST",
   url: "some.php",
   data: "name=John&location=Boston",
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

Now In some.php file you can access POST values like this:

$_POST['name']; // John
$_POST['location']; // Boston
Naveed
  • 41,517
  • 32
  • 98
  • 131
  • @NAVEED, so the first name is field in the database ? the second name is the id of the button? – cj333 Jan 28 '11 at 19:22
  • @cj333: No No. There is not any database field. I supposed that if you want to send two values(`name` and `location`) to a php script(`some.php`) to save in database then how you can do this using jQuery. Here in above jQuery code, `name` and `location` are same as you post a form with two fields(`name` and `location`) with `some.php` action in form. – Naveed Jan 28 '11 at 19:28
  • @NAVEED, in your example, if the html part is `` so the ajax part is `data: "name" + name +"&location="+location` ? – cj333 Jan 28 '11 at 19:39
  • @cj333: Yes you are near to point.. Calling above jQuery code in my answer is same as you submit following form: `
    `
    – Naveed Jan 28 '11 at 19:56
  • @cj333: You are welcome. I also learned jQuery from this site. Here are my some related questions which may help you. 1. http://stackoverflow.com/questions/3438123/how-to-submit-a-form-with-ajax-json 2. http://stackoverflow.com/questions/3506272/php-problem-submitting-form-in-ajax-json – Naveed Jan 28 '11 at 20:11
1

its the data that you are sending to the server for processing. so in your example you are sending the server:

field "name"
value "whatever is in the name variable"

so now the server can look up the the name field, use its value to do whatever it is you are asking the server to do.

Victor
  • 4,721
  • 1
  • 26
  • 31
0
$.ajax({
   type: "POST",
   url: "some.php",
   data: {
       name: "John",
       location: "Boston"
   },
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
});

if it is not helpful please check the fallowing link

aykut aydoğan
  • 328
  • 4
  • 9