Error sending form by POST

Asked

Viewed 34 times

1

I am experiencing a strange error. It is not really an error because the form sends the form but it changes one of the fields to 0 or 1. Here is the code.

<form method="POST" enctype="multipart/form-data">
   <div class="form-group col-lg-3" style="padding-left:0;">
      <label for="predifinicao">Nome predefinido</label>
      <input class="form-control" type="text" value="<?=$nome; ?>" name="titulo" id="predifinicao">
   </div>
   <div class="form-group col-lg-3" style="padding-left:0;">
      <label for="horario">Texto do Horário</label>
      <input class="form-control" type="text" value="<?=$horario; ?>" name="horario" id="horario">
   </div>
   <div class="form-group col-lg-6" style="padding-left:0;">
      <label for="capa">Imagem do programa</label>
      <div class="input-group">
         <input class="form-control" type="file" name="imagem" id="capa">
         <span class="input-group-addon label-primary" style="cursor:pointer;border:0;" data-target="#modal_preview_radio" data-toggle="modal"><i class="fa fa-eye"></i></span>
         <span class="input-group-addon label-danger" style="cursor:pointer;border:0;" title="Irá apagar a imagem atual do programa!" data-toggle="tooltip"><i class="fa fa-trash"></i></span>
      </div>
   </div>
   <div class="form-group">
      <input type="submit" class="btn btn-primary" name="submit" value="Alterar definições" style="width:200px;" />
   </div>
</form>

And now the PHP

if(isset($_POST['submit'])){
    $nome=$_POST['titulo'];
    echo $nome;
    $horario=$_POST['horario'];
    $update=mysqli_query($db, "UPDATE radio_predefinicao SET titulo = '$nome' AND horario = '$horario'") or die("Erro.");
    move_uploaded_file($_FILES['imagem']['tmp_name'], "../radio/programas/default/capa.jpg");
    echo "<script>window.location.href='index.php?pg=12';</script>";
}

The field $nome returns well in echo done there but he puts 0 if they are letters and 1 if they are numbers. What’s wrong with me?

  • Dude, you know these values by debugging or after giving a Ubmit.?

  • What values? Those of $_POST?

  • Dude adds this action="suapagina.php" and forehead again

  • if sending to the same page does not need the action

  • The value of input title, is a variable that takes which value?

  • Could you explain better what you are doing? What is your goal with this code? It is a form to edit data?

  • @Macario1983 It is a standard data editing form.

  • @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

  • 1

    I tested your code here and it worked normal

  • The UPDATE is the same as your?

  • @I_like_trains the variables that are in the attributes value are defined where?

  • 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;?>

  • @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

Show 8 more comments

1 answer

1

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");
  • I think that this question could serve with comment and no answer but I did not understand

  • Sorry, you’re right, because I was answering via cell phone, I’ll explain you better

  • Excuse the ignorance but where do you do the UPDATE?

  • Here I do the validation, if not have id otherwise... empty($plano['id']) ? inserirPlano($plano) : editarPlano($plano);

  • But after all, you did change your code as I indicated by adding that validation to use the isset?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.