2
I made the following form:
<html>
<body>
<form method="post" action="cadastro.php">
Informacoes <br>
Nome <input type="text" name="nome"> <br>
Endereco <input type="text" name="endereco"> <br>
<input type="submit" name="insert" value="add">
</form>
</body>
</html>
And I was wondering how I could record this in an XML file. Type:
<exemplo>
<cadastro>
<nome>
Nome Sobrenome
</nome>
<endereco>
Rua tal
</endereco>
</cadastro>
</exemplo>
Any example is welcome, I want to understand how you do but I find no right example on the web, thank you.
I tested the code to get the variables of the register and it was like this:
<?php
$nome = $_POST['nome'];
$email = $_POST['email'];
$telefone = $_POST['telefone'];
if (isset($_POST['botao'])){
#versao do encoding xml
$dom = new DOMDocument("1.0", "ISO-8859-1");
#retirar os espacos em branco
$dom->preserveWhiteSpace = false;
#gerar o codigo
$dom->formatOutput = true;
#criando o nó principal (root)
$root = $dom->createElement("agenda");
#nó filho (contato)
$contato = $dom->createElement("contato");
#setanto nomes e atributos dos elementos xml (nós)
$nome = $dom->createElement("nome", $nome);
$email = $dom->createElement("email", $email);
$telefone = $dom->createElement("telefone", $telefone);
#adiciona os nós (informacaoes do contato) em contato
$contato->appendChild($nome);
$contato->appendChild($email);
$contato->appendChild($telefone);
#adiciona o nó contato em (root) agenda
$root->appendChild($contato);
$dom->appendChild($root);
# Para salvar o arquivo, descomente a linha
$dom->save("contatos.xml");
#cabeçalho da página
header("Content-Type: text/xml");
# imprime o xml na tela
print $dom->saveXML();
}
?>
Please avoid long discussions in the comments; your talk was moved to the chat
– utluiz