Skip to content
Advertisement

Create an XML file in server using SimpleXML and jQuery Ajax

What I want to do surely can be accomplished but I’m doing something wrong: I want to create an XML file when I use an Ajax call. I got the following code (synthesized). Maybe this example doesn’t work, it’s just to exemplify:

HTML

<html>
<head>
  <!-- JQuery 1.7 included -->  
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
</head>
<body>
  <p>Create XML on server!</p>
  <form id="prog" method="POST">
    <input type="text" id="test" />
    <button id="submit" type="submit"> Create now! </button>
  </form>
  <script>
    jQuery.noConflict();
    jQuery(document).ready(function($){
        var test = $('#test').val();
 
        // When the button it's clicked:
        $('#submit').click(function () {
            $.ajax({
                // Este es el archivo PHP que procesa la información y envía el mail
                url: "createXML.php",
                type: "POST",
                data: test,
                success: function (html) {
                    // If succeed
                    if (html==1) {
                        alert("DONE!!");
                    }
                }
            });
        });
         
        // With this I cancel the default behaviour of the button so it doesn't submit by itself.
        return false;
    });

</script>
</body>
</html>

PHP in server

<?php
    echo "HI! Ajax arrived here and this code it's being executed!";
    
    //I load a string to be the contents of the XML file
    $exemel = simplexml_load_string('<example><simple>As simple as this!</simple></example>');
    
    // I save the file as following:
    $exemel->asXml('xml/DONE.xml');
    echo 1;
?>

In this example the PHP code works as is, the other one not. But in the whole context of my code, the Ajax call works, I have no doubt as it does everything else it must to just the creation of the XML code doesn’t. Having the very same PHP code as here, if I do the Ajax call the file it’s not created. If in a console I do

php createXML.php

The XML file it’s created successfully. So it’s not the PHP code and at the same time it’s not an error in my Ajax call because it does everything it should. What could be happening?

Edit

In my real code, in the PHP file in the server I do this:

$test = (isset($_GET['test'])) ? $_GET['test'] : null;
//If something fails, I add the error to an errors array:
$errors = array();
isset($errors);
if (!$test) $errors[count($errors)] = 'Something failed with the test!';
//If there are any errors
if (!$errors) {
    createXML ();
}

That createXML() function contains my previous code.

Advertisement

Answer

I’m so ashamed u.u The code I got was working good, but the folder where to write the file had no permissions for the user =/ I’m sorry… I double checked this, however even with a chmod 777 I couldn’t do it. It was an issue in the permissions with the group of the user… Sorry and thanks for helping me. Change permissions to the folder in Linux

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement