如何使用Jquery和Ajax将给定数据放入XML文件?

时间:2016-01-26 06:39:08

标签: php

我有一个模态,允许用户保存给定的数据。我想当用户点击“保存”按钮时,系统会将所有输入数据存储到XML文件

这是我的XML文件:

<Begin>
  <Result name="CNF-359-vu-01212016-4-09-21" desc="This is a sample description"/>
  <Result name="CNF-365-admin-01222016-2-39-21" desc="This is a sample description 2"/>
</Begin>

我的模特功能:

function addCNF(){

        //get the raw textdata of sample.xml
        $xmlRaw = file_get_contents(base_url(self::MANAGE_RESULT));

        //use the method to parse the data from xml

        $xml = simplexml_load_string($xmlRaw);

        if ($xml === false) {
            echo "Failed loading XML: ";
            foreach(libxml_get_errors() as $error) {
                echo "<br>", $error->message;
            }
            return false;
        } else {

            //insert to xml 
            $cnf = $xml->addChild('Result');
//          $cnf->addAttribute('name', '');
//          $cnf->addAttribute('desc', '');
            file_put_contents(self::MANAGE_RESULT, $xml->asXML()) or die("Error");
            formatXml(self::MANAGE_RESULT);
        }

        return "ok";
    }

我的控制器:

function addCNF(){
        if ($this->manageresult_model->addCNF() == "ok"){
            echo "Data save successfully!";
        } else {
                echo "Failed!"; 
            }
    }

我的观点:

$('#btnSaveChanges').on('click', function() {

    var post = '<?php echo base_url();?>index.php/manage_result/addCNF';
    $.ajax({
        type:'POST', cache:false,
        url : post,
        dataType: "text",
        success: function(result) {

            var xml = "";

            var name = $('#CNFName').val();
            var desc = $('#CNFDesc').val();
            xml += "\t name = '" + name + "' desc ='" + desc + "'>\n";          

        $('#id_content').append(xml);

        },
        error:function() {
            alert('Unable to perform the request, please refresh page and try again later!');
        }
    });
    $('#myModal2').modal('toggle');

});

我不知道我应该在View文件中做什么,以便系统将所有输入数据存储到XML文件中。请指教!

0 个答案:

没有答案