Create XML file on server using SimpleXML and jQuery Ajax

I need help, I hope someone can help me =) What I want to do, of course, can be done, but I'm doing something wrong: I want to create an XML file when I use the Ajax call. I got the following code (synthesized). Note that this example does not work, it just illustrates:

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 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 on the server

 <?php echo "HI! Ajax arrived here and this code it 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 it is, and the other does not. But in the whole context of my code, the Ajax call works, I have no doubt, because it does the rest that it only needs to create the XML code, no. Having the exact same PHP code as here, if I make an Ajax call to a file that he did not create ... If in the console I do

 php createXML.php 

The XML file that he created successfully. So this is not PHP code and at the same time it is not an error in my Ajax call, because it does everything that is needed. What can happen??? Of course, I missed something, THANKS !!!! =)

EDIT: In my real code in the PHP file on 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 (); } 

This createXML () function contains my previous code.

0
source share
2 answers

I am so ashamed uu The code that I had works well, but the folder where the file is written does not have user rights = / I'm sorry ... I checked this twice, however, even with chmod 777 I could not do it. It was a permissions issue with a user group ... Sorry and thank you for helping me. Change folder permissions on Linux

0
source

try it

 $xml = simplexml_load_string($_POST['test']); if(file_put_contents("xmlfile.xml",$xml->asXML())){ echo "xml file created successfully"; }else{ echo "failed to create file"; } 

also on the php side are you doing nothing with $ _POST ['test']? where is the xml line ...

+1
source

Source: https://habr.com/ru/post/1487203/


All Articles