0

I'm trying to update a XML file via jquery Ajax at client and php simple_XML at server.

My ajax code is:

$("input[type='radio']").click(function () {
            $().progress();
            $.ajax({
                type: "POST",
                url: "save.php",
                data: {name: $(this).attr('name'), value: $(this).val(), id: 13618},
            });
    });

in save.php i've:

$path="";
$xmlfile=$path.'vote/'.$_POST['id'].'.xml';
$xml = simplexml_load_file($xmlfile);
$xml->'a'.$_POST['name']=$_POST['value'];
$xml->asXML($path.'vote/'.$_POST['id'].'.xml');
return;

vote/13618.xml contains a template of XML tags each starting with an extra 'a'.

e.g.: for this fiddle, in my xml file, i've:

<posts>
    <a1president/>
    <a2vice-president/>
</posts>

but the php file is not updating the xml file. i'm unable to track the bug.

is there any error in my code? if yes where and how to fix?

RatDon
  • 3,403
  • 8
  • 43
  • 85

1 Answers1

1

You are firing asXML, but you don't return the value. If you do, you can check if the function returns false or true.

If it returns false, check the further response. Make sure you've enabled error reporting:

error_reporting(-1);
Stephan Vierkant
  • 9,674
  • 8
  • 61
  • 97
  • i guess, in my ajax code, there is no error (saw the same code working fine in a tutorial). i'm in doubt that the received data format is POST or any other like json. also `$xml->'a'.$_POST['name']` is valid or not. & `asXML` will output a XML file. there is no link to ajax with this. – RatDon Sep 04 '13 at 14:20
  • I can't see if that code is executed, because you only posted a small part of your `save.php`. Why don't you check it? Please check the response, you can't find an answer without debugging (guessing is not debugging ;-)). If you're using Chrome or Firefox, it's very easy to use the console, so use it. Return values (`var_dump`) to see what's happening. – Stephan Vierkant Sep 04 '13 at 14:25
  • `save.php` consists mainly that much code, rest are just for checking the post is `$_SERVER['HTTP_X_REQUESTED_WITH']`(from ajax request) or not. just security if anyone directly opens `save.php`. – RatDon Sep 04 '13 at 14:29
  • I'm not sure $_SERVER['HTTP_X_REQUESTED_WITH'] will work: http://stackoverflow.com/questions/2579254/php-does-serverhttp-x-requested-with-exist-or-not Why don't you just debug your code well? – Stephan Vierkant Sep 04 '13 at 14:33