Textarea list via jquery .ajax to php

Go To StackoverFlow.com

0

I have a textarea that I want to submit via ajax. When I try to output the value, I only get [object Object]

Jquery (ajax)

$("#insertAddresses").click(function() {

   $.ajax({
      type: "POST",
      url: "insertAddr.php",
      data: 'addresses=' +  
   }).done(function(list) {
      //getList();  // run query to get addresses and populate list
   });
});

PHP (i've tried)

$_POST['addresses']; 

or

$addresses = explode("\n", $_POST['addresses']);

Regardless of anything i've tried, always returns [object Object]

Help?!

2012-04-03 20:37
by Gene R
What is 'addresses=' + - Francisc 2012-04-03 20:39
We should see a data: { addresses : $('#textarea-id').text() } in your ajax function's data parameter - savinger 2012-04-03 22:23


1

Your serverscript is returning a json object, which is correctly recognized by JavaScript as an object. You can do a whole lot of things with that object, but you can't just put it on your website, as it is not html or text.

Here is a short description of json: http://en.wikipedia.org/wiki/JSON

I don't know how your data is structured, so i can't tell you how you can access your data. But in a json like this (example from wikipedia):

{
     "firstName": "John",
     "lastName" : "Smith",
     "age"      : 25,
     "address"  :
     {
         "streetAddress": "21 2nd Street",
         "city"         : "New York",
         "state"        : "NY",
         "postalCode"   : "10021"
     },
     "phoneNumber":
     [
         {
           "type"  : "home",
           "number": "212 555-1234"
         },
         {
           "type"  : "fax",
           "number": "646 555-4567"
         }
     ]
 }

You could, ie., excess the firstName simply with:

data.firstName

An voila, there is your excpected data.

2012-04-03 20:45
by marue


-1

You're data should be an object:

data: { adresses: "value" }
  • Just a little tip: the shorthand ajax-call for what you're doing in jQuery is $.post(.... and then you can lose the "type". Does exactly the same, but I think it's just a little neater.
2012-04-03 20:47
by Sander Bruggeman
Ads