使用SimpleXML和JQuery Ajax在服务器中创建XML文件

我需要一些帮助,我希望有人可以帮助我=)我想要做的确实可以完成,但我做错了:我想在使用Ajax调用时创建一个XML文件。 我得到了以下代码(合成)。 请注意,这个例子可能不起作用,只是为了举例说明:

HTML

      

Create XML on server!

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; });

PHP在服务器中

 <?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('As simple as this!'); // I save the file as following: $exemel->asXml('xml/DONE.xml'); echo 1; ?> 

在这个例子中,PHP代码按原样工作,另一个不是。 但是在我的代码的整个上下文中,Ajax调用是有效的,我毫不怀疑,因为它只是创建XML代码所必需的一切。 拥有与此处完全相同的PHP代码,如果我执行Ajax调用文件它不会被创建…如果在控制台中我做

 php createXML.php 

它成功创建的XML文件。 所以它不是PHP代码,同时它在我的Ajax调用中不是错误,因为它完成了应有的一切。 可能会发生什么? 当然我错过了什么,谢谢!!!! =)

编辑:在我的真实代码中,在服务器的PHP文件中我这样做:

 $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 (); } 

createXML()函数包含我以前的代码。

试试这个

 $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"; } 

另外在PHP端你不用$ _POST [‘test’]做任何事情? xml字符串是..

我很惭愧uu我得到的代码工作得很好,但是写文件的文件夹对用户没有权限= /对不起……我仔细检查过这个,但是即使用chmod 777我也不能做到了。 这是与用户组的权限中的一个问题…抱歉,感谢您的帮助。 更改Linux中文件夹的权限