tags:

views:

165

answers:

4

Thanks for all the great answers, this helps a lot!


Hello, I need some advice/help (obviously)

I'm pretty new to jquery/ajax in general but I have been able to do quite a bit so far. Here is what I am TRYING to do and then what I have managed to do so far.

I am trying to emulate the wordpress post categories. Where you are writing a post and then when you want to add it to a category it lets you just create new checkbox categories on the spot!

I have managed to get it working to where I send the request to a php script and it adds the record to my DB and then it appends the new checkbox to the bottom of my list. The issue I have is that I need to return the two values from PHP, the ID and the NAME. Right now I can return the name but I am not sure how to return them separately so that I can manipulate them in the JS.

I might be going about this in the wrong way completely. The idea is that I would create the new category records and then append them back to the category list using JS and then I would checkmark them and when I click "save" the values (IDs) would be sent to the current row (post) in the DB.

I hope i'm making some sense. If you have any questions please ask away and if you have any ideas or answers please let me know!

Thanks!

A: 

Well, I have and idea of what you are trying to do:

Until now, you were returning the ID of the newly created category in plaintext from the php to the calling javascript.

Now you want to return both the ID and the NAME of the newly created category.

There are two ways to do this: using XML or JSON. (on may argue there are more ways. and indded, there are.)

in XML:

<response>
  <ID>2</ID>
  <NAME>jquery</NAME>
</response>

in JSON:

{
  id : 2,
  name : "jquery"
}

parsing JSON in javascript is a trivial matter. parsing XML can be done using jQuery, just look up the jQuery ajax docs.

Cheers,

jrh.

Here Be Wolves
For simple cases JSON is better. XML is preferred when you plan intercommunications between resources on smth. like client-server application.
Jet
A: 

The most common method to return values is to use a JSON string JSON wikipedia

and then do

var p = eval(response.txt);
alert(p.var1);
alert(p.var2);
nullptr
A: 

Assuming $id and $name are the values you want to return:

<?php

$id = 2;
$name = "jquery";

echo json_encode (array("id" => $id, "name" => $name));

?>

Then use any of the other answers for manipulating these values in Javascript.

Jordan S. Jones
+3  A: 

Try returning the text as JSON. Depending on the jQuery AJAX method you are using you can set a custom callback (a function that gets called after the first function has returned).

For example if you're using the $.post method you could do something like this:

backend.php:

$return_value = array();
$return_value['value1'] = 'data 1';
$return_value['value2'] = 'data 2';
echo json_encode($return_value);

frontend.js:

$.post("backend.php", '',
    function(data) {
        alert(data.value1); // Would show data 1
        alert(data.value2); // Would show data 2
}, "json");
Ben