HTML / PHP表单到XML

I'm looking to create an .XML file from the inputs of an HTML/PHP form. I know that there are some posts about that, but I couldn't find the solution to my case.

So, I have a form

<form action="" method="post" id="sett">
<input type="text" name="name" id="name" placeholder="Name"/>
<input type="text" name="age" id="age" placeholder="Age" />
<input type="text" name="country" id="country" placeholder="Country"/>
<button onclick=send()>Send</button>
</form>

If I introduce John as Name, 18 as age and US as Country, I want to get

<?xml version="1.0"?>
<sett>
    <name>John</name>
    <age>18</age>
    <country>US</country>
</sett>

A way to do it is

$xml = "<sett>
";
$xml .= "\t\t<name>" . $_POST['name'] . "</name>
"; 
$xml .= "\t\t<age>" . $_POST['age'] . "</age>
"; 
$xml .= "\t\t<country>" . $_POST['country'] . "</country>
"; 
$xml .= "</sett>";
$fp = fopen("input.xml","wb");
fwrite($fp,$xml);
fclose($fp); 

The problem is that I have a large form, not like the one in the example, and it's a bit rought to do it like in the way I just explained. I want to do it dynamically with JavaScript/jQuery/AJAX/json. I found that I can do it sending the array with json to another script or .PHP and then create the .XML

<script>
function send(){
var array= //Array created with the POST/input of the form

$.ajax({
    url: 'createxml.php',
    type: "POST",
    data: JSON.stringify(array),
    contentType: "application/json",
    success: function(data){
    alert('Datos enviados correctamente');
    }
});
}
</script>

createxml.php

<?php $array = json_encode($_POST['array']); ?>
<?php 
for($i=0;$i < count($array);$i++)  
//Create the .XML from the array
?>

I need help with the missing parts (and I don't know if the jQuery/AJAX code is correct): how to create an array from the POST/input of the form, send it to createxml.php, and create the .XML from the array we sent.

You can iterate in PHP as well, creating the tags and values based on the form data

$xml = "<sett>
";

foreach ($_POST as $key => $value) { 
    $xml .= "<$key>$value</$key>";
}

$xml .= "</sett>";

Also, remove the send() function and the inline event handler from the form, and just do

$('#sett').on('submit', function(e) {
    e.preventDefault();
    $.post('createxml.php', $(this).serialize(), function(data) {
        alert('Datos enviados correctamente');
    });
});