0

How is this done to generate an XML file based on this data?

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

How will the PHP file look like to be able to retrive the information and then save it to a file in the same folder?

3
  • why not pass the info by JSON then on the php file, create the file structure with the information passed? Commented Apr 29, 2013 at 13:34
  • Do a var_dump($_POST); in your savequiz.php and take it from there. Commented Apr 29, 2013 at 13:34
  • Okey, I'm quite new to JSON and PHP so I'm not quite sure how its done.. Commented Apr 29, 2013 at 13:37

1 Answer 1

1

Keep tranfering data as small as possible to increase performance; I suggest you to do it like:

JS:

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

savequiz.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.

and try do more research to know how to create XML file and Working with files using PHP.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.