That XML is a lot of unnecessary work. If you're running with PHP5,
simply print on success:
echo json_encode(array('success' => true, message => ''));
or on failure:
echo json_encode(array('success' => false, message => ''));
And for the jQuery bit:
$.post('somurl.php', function (data){
if (data.success){
// Code for successful comment post
} else {
// Code for failed comment post
}
}, 'json');
On Jan 30, 12:42 pm, GreatBigBore <[email protected]> wrote:
> Have your PHP script return an XML document, and then parse that XML
> with JQuery:
>
> PHP:
>
> /*
> <FunctionResult>
> <FunctionStatus success="1" message="Request processed" />
> </FunctionResult>
> */
> $document = DOMImplementation::createDocument();
> $document->formatOutput = true;
> $document->preserveWhitespace = true;
>
> $functionResult = $document->createElement('FunctionResult');
> $document->appendChild($functionResult);
>
> $functionStatus = $document->createElement('FunctionStatus');
> $functionResult->appendChild($functionStatus);
>
> $success = true;
> $message = 'Request successful';
>
> ...Do stuff that changes $success and $message if necesssary...
>
> $functionStatus->setAttribute('success', $success);
> $functionStatus->setAttribute('message', addslashes($message));
>
> // Now respond to the requestor
> Header("Content-type: text/xml", 1);
> echo $document->saveXML();
>
> Javascript:
>
> $.post('http://myserver.com/ajax.php',
> { 'function' : 'getEfforts',
> 'parameter1' : something
> },
>
> function(xml) {
> /*
> <FunctionResult>
> <FunctionStatus success="1" message="Request
> processed" />
> </FunctionResult>
> */
> var statusNode = $('FunctionStatus', xml).get(0);
> var success = intToBool(statusNode.getAttribute('success'));
> if(success === true) {
> } else {
> }
> }
> );