0

如何根据这些数据生成 XML 文件?

$("#savequiz").click(function(){
    var text = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><quiz>";
    // save the general settings
    text += "<title><![CDATA[" + $('input[name="title"]').val() + "]]></title>";
    text += "<time><![CDATA[" + $('input[name="time"]').val() + "]]></time>";

    text += "<singlechoice><![CDATA[" + $('input[name="singlechoice"]').val() + "]]></singlechoice>";
    text += "<multiplechoice><![CDATA[" + $('input[name="multiplechoice"]').val() + "]]></multiplechoice>";
    text += "<nextbutton><![CDATA[" + $('input[name="nextbutton"]').val() + "]]></nextbutton>";
    text += "<prevbutton><![CDATA[" + $('input[name="prevbutton"]').val() + "]]></prevbutton>";
    text += "<finishbutton><![CDATA[" + $('input[name="finishbutton"]').val() + "]]></finishbutton>";
    text += "<startbutton><![CDATA[" + $('input[name="startbutton"]').val() + "]]></startbutton>";
    text += "<reviewbutton><![CDATA[" + $('input[name="reviewbutton"]').val() + "]]></reviewbutton>";
    text += "<resultscreentitle><![CDATA[" + $('input[name="resultscreentitle"]').val() + "]]></resultscreentitle>";
    text += "<resultscreenresultline><![CDATA[" + $('input[name="resultscreenresultline"]').val() + "]]></resultscreenresultline>";
    text += "<welcometext><![CDATA[" + $('textarea[name="welcometext"]').val() + "]]></welcometext>";
    for(var i = 0; i < questions.length; i++) {
        var q = questions[i];
        var answers = q.getAnswers();
        text += "<question><![CDATA[" + q.getQuestion() + "]]>"; 
        for(var n = 0; n < answers.length; n++){
            text += "<answer correct='" + answers[n].getCorrect() + "'>";
            text += "<![CDATA[" + answers[n].getAnswer() + "]]></answer>";  
        };
        text +="</question>";
    };
    text +="</quiz>";

    // save to file:
    $.ajax({
        type: 'POST',
        url: "savequiz.php",
        dataType: 'json',
        data: "filename=" + filename + ".xml" + "&quiz=" + text,
        success: function (text) {
            //console.log(text.errors);
            //console.log(text.feedback);
            if(text.feedback == "saved") respondChangedState(false);
        }
    });

PHP 文件看起来如何能够检索信息,然后将其保存到同一文件夹中的文件中?

4

1 回答 1

1

保持传输数据尽可能小以提高性能;我建议你这样做:

JS:

//passin data via ajax
 ...
url: "savequiz.php",
data:{
   title : $(input[name="title"]).val(),
   name : $(input[name="name"]).val(),
   //and so on ...
}

保存测验.php:

 $title = $_POST['title'];
 $name = $_POST['name'];
 //and so on ...

 //and HERE is the proper place to create XML file using above $_POST data
 //and save it to a folder.

并尝试做更多研究以了解如何创建 XML 文件和使用 PHP处理文件。

于 2013-04-29T13:49:27.633 回答