As I reported
<?php
include_once($_SERVER['DOCUMENT_ROOT']."/cobranca/plano/funcoes.php");
include_once($_SERVER['DOCUMENT_ROOT']."/cobranca/utils/global_funcoes.php");
$dadosValido = true;
$arrayMensagemErros = array();
$plano = (isset($_GET['id'])) ? carregarDadosPlanoBanco($_GET['id']) : carregarDadosPlanoSessao();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(!validarCamposPreenchidosPlano($arrayMensagemErros)) {
if (isset($_POST['id'])) {
if (!is_numeric($_POST['id'])) {
die("O código é inválido");
}
$plano['id'] = $_POST['id'];
}
$plano['tipo_cobranca'] = limpaCampo($_POST['tipo_cobranca']);
$plano['valor'] = limpaCampo($_POST['valor']);
$plano['nome'] = limpaCampo($_POST['nome']);
empty($plano['id']) ? inserirPlano($plano) : editarPlano($plano);
header("Location: list.php");
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Cadastro de planos de cobrança</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="../css/style.css" />
<script src="main.js"></script>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="POST">
<div style="width: 600px;" class="elementoCentralizado">
<h1 class="elementoCentralizado">Cadastro dos planos de cobrança</h1>
<table border="1" style="margin-left: auto;margin-right: auto">
<tr>
<td>
<label for="nome">Nome:</label>
</td>
<td colspan="2">
<input type="text" name="nome" value="<?php echo preencheValue($plano['nome']);?>"/>
<span><?php echo retornaChave($arrayMensagemErros, 'nome'); ?></span>
</td>
</tr>
<tr>
<td>
<label for="tipo_cobranca">Tipo de cobrança:</label>
</td>
<td colspan="2">
<select name="tipo_cobranca" id="tipo_cobranca" value="<?php echo preencheValue($plano['nome']);?>">
<option value="M" <?php echo optionSelector($plano['tipo_cobranca'], "M"); ?>>Mensal</option>
<option value="A" <?php echo optionSelector($plano['tipo_cobranca'], "A"); ?>>Anual</option>
</select>
<span><?php echo retornaChave($arrayMensagemErros, 'tipo_cobranca'); ?></span>
</td>
</tr>
<tr>
<td>
<label for="valor">Valor:</label>
</td>
<td colspan="2">
<input type="text" name="valor" value="<?php echo preencheValue($plano['valor']);?>">
<span><?php echo retornaChave($arrayMensagemErros, 'valor'); ?></span>
</td>
</tr>
<tr>
<td class="elementoCentralizado">
<input type="reset" value="Limpar">
</td>
<td class="elementoCentralizado">
<div class="botao">
<a href="list.php">Cancelar</a>
</div>
</td>
<td class="elementoCentralizado">
<?php if (!empty($plano['id'])) :
echo "<input type='hidden' name='id' value='{$plano['id']}'>";
endif; ?>
<input type="submit" value="Salvar">
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Functions to search from the bank and if there is any validation problem, keep the data in the session
function carregarDadosPlanoBanco($codigo) {
$conexao = conectarAoBanco();
$sql = "SELECT * FROM planos WHERE id = " . $codigo . ";";
$result = mysqli_query($conexao, $sql);
return mysqli_fetch_assoc($result);
}
function carregarDadosPlanoSessao() {
$plano['id'] = isset($_POST['id']) ? $_POST['id'] : "";
$plano['nome'] = isset($_POST['nome']) ? $_POST['nome'] : "";
$plano['tipo_cobranca'] = isset($_POST['tipo_cobranca']) ? $_POST['tipo_cobranca'] : "";
$plano['valor'] = isset($_POST['valor']) ? $_POST['valor'] : "";
return $plano;
}
Method to validate the data, detail, step the vector by reference not to lose it and keep the values.
function validarCamposPreenchidosPlano(&$arrayMensagemErros) {
$dadoInvalido = false;
if (empty($_POST['nome'])) {
$arrayMensagemErros['nome'] = "Preencha o campo Nome";
$dadoInvalido = true;
}
if (empty($_POST['tipo_cobranca'])) {
$arrayMensagemErros['tipo_cobranca'] = "Preencha o campo Tipo de cobrança";
$dadoInvalido = true;
}
if (empty($_POST['valor'])) {
$arrayMensagemErros['valor'] = "Preencha o campo Valor";
$dadoInvalido = true;
}
return $dadoInvalido;
}
And finally, if the insertion or editing is correct, I use the locate method
header("Location: list.php");
Dude, you know these values by debugging or after giving a Ubmit.?
– Macario1983
What values? Those of
$_POST
?– I_like_trains
Dude adds this
action="suapagina.php"
and forehead again– Macario1983
if sending to the same page does not need the action
– Wees Smith
The
value
of input title, is a variable that takes which value?– Wees Smith
Could you explain better what you are doing? What is your goal with this code? It is a form to edit data?
– Macario1983
@Macario1983 It is a standard data editing form.
– I_like_trains
@I_like_trains, I recently made a data insertion and editing form on the same page. What I did was the following, when loading the page, I search the data in the database and forward for each variable the value. I’ll edit my answer so you understand
– Macario1983
I tested your code here and it worked normal
– Wees Smith
The
UPDATE
is the same as your?– I_like_trains
@I_like_trains the variables that are in the attributes
value
are defined where?– Wees Smith
One thing I did here to run, I added the following validation in your form, was in the values setar a validation of this type:
<?php if (isset($nome)) $nome;?>
– Macario1983
@I_like_trains in my example I left everything well separated in other files, I’m reapplying
PHP
, because I only saw in school and college the complete example of my example system is in:https://github.com/macario1983/CobrancaPHP
– Macario1983