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>\n";
$xml .= "\t\t<name>" . $_POST['name'] . "</name>\n";
$xml .= "\t\t<age>" . $_POST['age'] . "</age>\n";
$xml .= "\t\t<country>" . $_POST['country'] . "</country>\n";
$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.