Você está na página 1de 4

<!

DOCTYPE html>
<html>
<head>
<title>Visualizar Imóveis</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}

th, td {
padding: 8px;
text-align: left;
border-bottom: 1px solid #ddd;
}
</style>
</head>
<body>
<h1>Imóveis Cadastrados</h1>

<table>
<tr>
<th>Título</th>
<th>Descrição</th>
<th>Preço</th>
</tr>

<?php
// Verifica se o arquivo XML existe
if (file_exists('imovel.xml')) {
// Carrega o arquivo XML
$xml = simplexml_load_file('imovel.xml');

// Exibe os dados do imóvel na tabela


foreach ($xml->children() as $imovel) {
echo "<tr>";
echo "<td>" . $imovel->titulo . "</td>";
echo "<td>" . $imovel->descricao . "</td>";
echo "<td>" . $imovel->preco . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='3'>Nenhum imóvel cadastrado</td></tr>";
}
?>
</table>
</body>
</html>

<?php
// Verifica se os dados foram enviados via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Verifica se todos os campos obrigatórios foram preenchidos
if (isset($_POST['titulo']) && isset($_POST['descricao']) &&
isset($_POST['preco'])) {
// Cria uma nova instância do SimpleXMLElement
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?
><imovel></imovel>');

// Adiciona os campos do imóvel ao documento XML


$xml->addChild('titulo', $_POST['titulo']);
$xml->addChild('descricao', $_POST['descricao']);
$xml->addChild('preco', $_POST['preco']);

// Salva o documento XML em um arquivo ou exibe na tela

// Exemplo: Salva o XML em um arquivo chamado imovel.xml


$xml->asXML('imovel.xml');

echo 'Dados enviados com sucesso!';


} else {
echo 'Por favor, preencha todos os campos obrigatórios';
}
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Cadastro de Imóveis</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
margin: 0;
padding: 20px;
}

h1 {
text-align: center;
}

form {
max-width: 400px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

label {
display: block;
margin-bottom: 10px;
font-weight: bold;
}

input[type="text"],
input[type="number"],
textarea {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
}

input[type="submit"] {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
}

input[type="submit"]:hover {
background-color: #45a049;
}

p.download-link {
text-align: center;
margin-top: 20px;
}

p.download-link a,
p.view-link a {
display: inline-block;
padding: 10px 20px;
background-color: #4CAF50;
color: white;
text-decoration: none;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>Cadastro de Imóveis</h1>

<form method="POST" action="">


<label for="titulo">Título:</label>
<input type="text" name="titulo" required>

<br>

<label for="descricao">Descrição:</label>
<textarea name="descricao" required></textarea>

<br>

<label for="preco">Preço:</label>
<input type="number" name="preco" required>

<br>

<input type="submit" value="Enviar">


</form>

<?php
// Verifica se o arquivo XML existe
if (file_exists('imovel.xml')) {
echo '<p class="download-link"><a href="imovel.xml" download>Baixar
XML</a></p>';
echo '<p class="view-link"><a href="visualizar.php"
target="_blank">Visualizar XML</a></p>';
}
?>
</body>
</html>

Você também pode gostar