使用SimpleXML和JQuery Ajax在服务器中创建XML文件 [英] Create an XML file in server using SimpleXML and JQuery Ajax

查看:88
本文介绍了使用SimpleXML和JQuery Ajax在服务器中创建XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need some help, I hope someone can help me =) 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). Please note 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

<?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;
?>

在此示例中,PHP代码按原样工作,其他代码则不然.但是在我的代码的整个上下文中,Ajax调用都能正常工作,我毫不怀疑,因为它完成了XML代码创建所不具备的所有其他工作. 具有与此处相同的PHP代码,如果我执行Ajax调用,则不会创建该文件...如果在控制台中,我会

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

已成功创建XML文件.因此,它不是PHP代码,同时也不是我的Ajax调用中的错误,因为它可以完成所有应做的事情.可能会发生什么???我肯定会丢失一些东西,谢谢!!!! =)

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??? Surely I'm missing something, THANKS!!!! =)

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

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

该createXML()函数包含我之前的代码.

That createXML() function contains my previous code.

推荐答案

我很as愧 我得到的代码运行良好,但是用于写文件的文件夹没有用户权限=/对不起...我再次检查了一下,但是即使使用chmod 777,我也无法做到.这是用户组权限中的一个问题.对不起,感谢您的帮助. 在Linux中更改对该文件夹的权限

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

这篇关于使用SimpleXML和JQuery Ajax在服务器中创建XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆