I click the save button to register a product in my database and nothing happens HTML + PHP

Asked

Viewed 158 times

0

I made a code in html + php to register products in my database, the connection class is working because the client registration works and I use the same include_once("connection.php"). The form is as post and the action for my record file.php.

When I click on Btnsalvar nothing happens, no error appears, nor register in the bank. Follow below the codes:

pgcadpro.php file (product registration)

<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
include_once("conexao.php");
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <div class="site-logo mr-auto w-25"><title>ImperialCalçados</title></div>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="https://fonts.googleapis.com/css?family=Muli:300,400,700,900" rel="stylesheet">
    <link rel="stylesheet" href="fonts/icomoon/style.css">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/jquery-ui.css">
    <link rel="stylesheet" href="css/owl.carousel.min.css">
    <link rel="stylesheet" href="css/owl.theme.default.min.css">
    <link rel="stylesheet" href="css/owl.theme.default.min.css">
    <link rel="stylesheet" href="css/jquery.fancybox.min.css">
    <link rel="stylesheet" href="css/bootstrap-datepicker.css">
    <link rel="stylesheet" href="fonts/flaticon/font/flaticon.css">
    <link rel="stylesheet" href="css/aos.css">
    <link rel="stylesheet" href="css/style.css">
    
    <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    <script>
      $(document).ready(function(){
        $.get("listacategorias.php",{}).done(function(dados){
          $("#sel_categoria").html(dados);
        }); 
            })             
    </script>
    <!-- cadcliente script -->
  </head>
  <div class="site-section bg-image overlay" style="background-image: url('images/vitrini.jpg');">
      <div class="container">
        <div class="row justify-content-center align-items-center">
          <div class="col-md-8 text-center testimony">
          
          </div>
        </div>
      </div>
    </div>
  <div class="site-section" id="teachers-section">
      <div class="container">
        <div class="row mb-5 justify-content-center">
          <div class="col-lg-7 mb-5 text-center"  data-aos="fade-up" data-aos-delay="">
            <h2 class="section-title">Cadastrar Produto</h2>
          </div>
        </div>
        <div class="row">
        <form class="form-horizontal" >
<form class="form-horizontal" method="post" action="gravaproduto.php">
</div>
<div class="form-group">
  <label class="col-md-4 control-label" for="txtproduto">Produto : </label>  
  <div class="col-md-6">
  <input id="txtdescricao" name="txtdescricao" type="text" placeholder="Descrição do Produto" class="form-control input-md" required="">
  </div>
</div>
<div class="form-group">
  <label class="col-md-4 control-label" for="txtgrupo">Categoria : </label>
  <div class="col-md-4">
  <select name="sel_categoria" id="sel_categoria"  class="form-control">
  </select>
  </div>
</div>
 <div class="form-group">
  <label class="col-md-4 control-label" for="txtcodigo_unidade_id">Valor : </label>
  <div class="col-md-4">
    <form name="teste">
      <input type="text" id="txtvalor" name="txtvalor" onkeyup="maskIt(this,event,'###.###.###,##',true,{pre:'R$'})"/>
    </form>
  </div>
  </div>
  <div class="form-group">
  <label class="col-md-4 control-label" for="btnsalvar"></label>
  <div class="col-md-8">
    <button id="btnsalvar" name="btnsalvar" type="Submit" class="btn btn-primary">Salvar</button>
    <button id="btncancelar" name="btncancelar" class="btn btn-danger">Cancelar</button>
    <br/><br/><br/><br/><br/><br/>
  </div>
</div>
</div>
</fieldset>
</form>
</body>
    <div class="site-section bg-image overlay" style="background-image: url('images/vitrini.jpg');">
      <div class="container">
        <div class="row justify-content-center align-items-center">
          <div class="col-md-8 text-center testimony">
          
          </div>
        </div>
      </div>
    </div>
    <div class="site-section pb-0">
      <div class="future-blobs">
        <div class="blob_2">
          <img src="images/blob_2.svg" alt="Image">
        </div>
        <div class="blob_1">
          <img src="images/blob_1.svg" alt="Image">
        </div>
      </div>
        <div class="row pt-5 mt-5 text-center">
          <div class="col-md-12">
            <div class="border-top pt-5">
            <p>
        <!-- Link back to Colorlib can't be removed. Template is licensed under CC BY 3.0. -->
        Copyright &copy;<script>document.write(new Date().getFullYear());</script> All rights reserved | This template is made with <i class="icon-heart" aria-hidden="true"></i> by <a href="https://colorlib.com" target="_blank" >Colorlib</a>
        <!-- Link back to Colorlib can't be removed. Template is licensed under CC BY 3.0. -->
      </p>
            </div>
          </div>
          
        </div>
      </div>
  </div> <!-- .site-wrap -->
  <script src="js/jquery-3.3.1.min.js"></script>
  <script src="js/jquery-migrate-3.0.1.min.js"></script>
  <script src="js/jquery-ui.js"></script>
  <script src="js/popper.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="js/owl.carousel.min.js"></script>
  <script src="js/jquery.stellar.min.js"></script>
  <script src="js/jquery.countdown.min.js"></script>
  <script src="js/bootstrap-datepicker.min.js"></script>
  <script src="js/jquery.easing.1.3.js"></script>
  <script src="js/aos.js"></script>
  <script src="js/jquery.fancybox.min.js"></script>
  <script src="js/jquery.sticky.js"></script>
  <script src="js/main.js"></script>
</html>

File records product:

<?php
    $conn= new PDO("mysql:host=localhost:3306;dbname=loja","root","aula");

    $descricao=$_POST["txtdescricao"];
    $idcategoria=$_POST["sel_categoria"];
    $valor=$_POST["txtvalor"];

    $stmt = $conn->prepare('Insert into loja.produto(descricao,valor,idcategoria)
    Values(:par_descricao,:par_valor,:par_idcategoria)');

    $dados=array(":par_descricao"=>$descricao,":par_valor"=>$valor,
    ":par_idcategoria"=>$idcategoria);
    
    $result = $stmt->execute($dados);
    
    if($result){
        echo ("Produto inserido com sucesso");
    }
    else{
        echo("Erro");
    } ?>

    <div class="form-group">
<footer id="footer">
    <div class="container">
      <div class="row">
        <div class="col-md-12">
          <div class="copyright">
            <div class="form-group caixa-pesquisa-div text-center">
            &copy; Copyright <strong>Imperial Calçados</strong>. Direitos Reservados <br /><br/>
                <input type="button" value="Voltar" onClick="history.go(-1)"style="color: #FFFFFF; background-color: #CC0000"> 
                <input type="button" value=" Sair  " onCLick="window.location.href = 'sair.php'" style="color: #FFFFFF; background-color: #CC0000"> 
              </div>
          </div>          
        </div>
      </div>
    </div>
</footer>

Thank you!

1 answer

0


You have created multiple tags <form> (one inside the other) in your code. Keep only one, this one:

<form class="form-horizontal" method="post" action="gravaproduto.php">

Ps: try to close it properly with just one </form>

This tag form here is the problem, change it by a div:

<form name="teste">
  <input type="text" id="txtvalor" name="txtvalor" onkeyup="maskIt(this,event,'###.###.###,##',true,{pre:'R$'})"/>
</form>
  • Thanks Alexandre, I made the change but not yet saved in the database, now it updates the page, and using the developer tool (F12) vi that gave error in line DOCTYPE> Error: unchecked Runtime lasterror the message port closed before a Sponse was Received

  • Now it worked, was still with a lost form open, Obrgado

  • I’m glad it worked out.

  • Alexandre, I’m blocked from asking questions but I only have a percussion with -2 because this as duplicate, I can not exclude because it says that people have invested time with her and helped some... what to do?

Browser other questions tagged

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