Duplication of PHP data

Asked

Viewed 70 times

-1

I have a very large page (Main software flow). I’m experiencing a problem that rarely happens, but sometimes the request is duplicated, as if I had sent twice the same form, only no, I just clicked on send and I wait for the php answer to tell me the success. This is a problem. Follow the html code:

<form name="frmpedidos" data-toggle="validator" method="POST" action="?folder=pedidos/&file=ff_ins_pedidos&ext=php" role="form" onsubmit="return validaDetalhe()">
  <div class="row">
    <div class="col-lg-12">
      <div class="col-md-6 col-lg-6">
        <div class="form-group row">
            <label class="col-lg-2 form-control-label">*Data:</label>
            <div class="col-lg-10 ">
              <input type="text" name="txtdata" class="form-control datas_pedidos" placeholder="dd-mm-aaaa" required  id="datepicker">
              <div class="help-block with-errors"></div>
            </div>
          </div>
        </div>
        <div class="col-md-6 col-lg-6">
          <div class="form-group row">
            <label class="col-lg-2 form-control-label">*Horário:</label>
            <div class="col-lg-10">
              <!-- <input type="time" name="txthorario" class="form-control datas_pedidos" placeholder="dd-mm-aaaa" required> -->
              <select class="form-control select-basic" type="text"  name="txthorario" id="txthorario" required >
                <option value="">Selecione...</option>
                <option value="01:00 - 02:00">01:00 - 02:00</option>
                <option value="02:00 - 03:00">02:00 - 03:00</option>
                <option value="03:00 - 04:00">03:00 - 04:00</option>
                <option value="04:00 - 05:00">04:00 - 05:00</option>
                <option value="05:00 - 06:00">05:00 - 06:00</option>
                <option value="06:00 - 07:00">06:00 - 07:00</option>
                <option value="07:00 - 08:00">07:00 - 08:00</option>
                <option value="08:00 - 09:00">08:00 - 09:00</option>
                <option value="09:00 - 10:00">09:00 - 10:00</option>
                <option value="10:00 - 11:00">10:00 - 11:00</option>
                <option value="11:00 - 12:00">11:00 - 12:00</option>
                <option value="12:00 - 13:00">12:00 - 13:00</option>
                <option value="13:00 - 14:00">13:00 - 14:00</option>
                <option value="14:00 - 15:00">14:00 - 15:00</option>
                <option value="15:00 - 16:00">15:00 - 16:00</option>
                <option value="16:00 - 17:00">16:00 - 17:00</option>
                <option value="17:00 - 18:00">17:00 - 18:00</option>
                <option value="18:00 - 19:00">18:00 - 19:00</option>
                <option value="19:00 - 20:00">19:00 - 20:00</option>
                <option value="20:00 - 21:00">20:00 - 21:00</option>
                <option value="21:00 - 22:00">21:00 - 22:00</option>
                <option value="22:00 - 23:00">22:00 - 23:00</option>
                <option value="23:00 - 24:00">23:00 - 00:00</option>
                <option value="00:00 - 01:00">00:00 - 01:00</option>
              </select>
              <div class="help-block with-errors"></div>
            </div>
          </div>
        </div>
        <div class="col-lg-12" >
          <div class="form-group row col-lg-12" >
              <label>*Deseja Retirar na Loja? (Isento de Taxas de Entrega)</label>
              <label class="radio-inline">
                <input type="radio" name="rdRetirar" id="rdRetirar" value="nao"  required> NÃO
              </label>
              <label class="radio-inline">
                <input type="radio" name="rdRetirar" id="rdRetirar" value="sim"  onload="RetirarLoja()"> SIM
              </label>
              <div class="help-block with-errors"></div>
          </div>
        </div>

      <hr class="hr">
      <div class="panel panel-default">
        <div class="panel-body">
          <div class="panel-group" id="accordion">
            <div class="panel panel-default">
              <div class="panel-heading">
                <h4 class="panel-title">
                  Remetente
                </h4>
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
                  <li class="fa fa-minus-square fa-2x navbar-right minimizar"  alt="" width="30px" height="30px"></li>
                </a>
              </div>
              <div id="collapseOne" class="panel-collapse collapse in">
                <div class="panel-body">
                  <div class="col-md-12 col-lg-6">
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Nome:</label>
                      <div class="col-sm-8">
                        <select name="selremetente" id="selremetente" class="select-tag form-control" maxlenght="45" style="width: 100%;" onChange="buscaremetente()" required>
                          <option value="">Escolha um cliente...</option>
                          <?php
                            $sql_sel_clientes_preparado->execute();

                            while($sql_sel_clientes_dados = $sql_sel_clientes_preparado->fetch()){
                          ?>
                          <option value="<?php echo $sql_sel_clientes_dados['id'];?>"><?php  echo $sql_sel_clientes_dados['nome'];?></option>
                          <?php
                            }
                          ?>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">E-mail:</label>
                      <div class="col-sm-8">
                        <input type="email" id="txtemail" name="txtemail" class="form-control" maxlength="70" placeholder="[email protected]">
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Telefone Fixo:</label>
                      <div class="col-sm-8">
                        <input type="tel" name="txttelefone1" id="telefone1" class="form-control" maxlength="20" placeholder="34391090" pattern="^([0-9]{1,20})$" required>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Telefone Celular:</label>
                      <div class="col-sm-8">
                        <input type="tel" name="txttelefone2" id="telefone2" class="form-control" maxlength="20" pattern="^([0-9]{1,20})$" placeholder="99445218123">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Estado:</label>
                      <div class="col-sm-8">
                        <select name="selestado" id="selEstado" class="select-basic form-control" style="width: 100%;" onChange="mostrarcidade()">
                          <option value="">Escolha um Estado..</option>
                          <?php
                            $sql_sel_estados_preparado->execute();

                            while($sql_sel_estados_dados = $sql_sel_estados_preparado->fetch()){
                          ?>
                          <option value="<?php echo $sql_sel_estados_dados['id'];?>"><?php  echo $sql_sel_estados_dados['nome'];?></option>
                          <?php
                            }
                          ?>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-12 col-lg-6">
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Cidade:</label>
                      <div class="col-sm-8">
                        <select name="selcidade" id="selCidade" class="select-basic form-control" style="width: 100%;" onchange="mostrarbairro()">
                          <option value=''>Escolha um Estado Primeiro...</option>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Bairro:</label>
                      <div class="col-sm-8">
                        <select name="selbairro" id="selBairro" class="select-basic form-control" style="width: 100%;">
                          <option value=''>Escolha uma Cidade Primeiro...</option>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Logradouro:</label>
                      <div class="col-sm-8">
                        <input type="text" id="txtlogradouro" name="txtlogradouro" class="form-control" placeholder="Marques de Olinda" maxlength="45">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Número:</label>
                      <div class="col-sm-8">
                        <input type="text" id="txtnumero" name="txtnumero" class="form-control" maxlength="6" placeholder="1222">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">CPF:</label>
                      <div class="col-sm-8">
                        <input type="text" id="cpf" name="txtcpf" class="form-control" maxlength="11" pattern="^[0-9]{3}[0-9]{3}[0-9]{3}[0-9]{2}$" placeholder="01234567890">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                  <!--/. div col-md-12 col-lg-6  -->
                  <div class="form-group row col-lg-12">
                    <label class="col-sm-2 form-control-label" >Comentário / Complemento:</label>
                    <div class="col-sm-10">
                      <textarea type"text" name="txtacomplemento" id="complemento" class="form-control" placeholder="Comentário/Complemento do endereço do Remetente..."></textarea>
                    </div>
                  </div>
              </div>
            </div>

          </div>
          <!-- /.panel-collapse collapse in -->
          <div class="panel panel-default">
              <div class="panel-heading">
                <h4 class="panel-title">
                  Destinatário
                </h4>
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                <li class="fa fa-minus-square fa-2x navbar-right minimizar"  alt="" width="30px" height="30px"></li>
                </a>
              </div>
              <div id="collapseTwo" class="panel-collapse collapse">
                <div class="panel-body">
                  <div class="col-lg-12">
                    
                    <div class="form-group row">
                      <label class="col-sm-5 form-control-label">* O Destinatário é o mesmo que o Remetente?:</label>
                      <div class="col-sm-7">
                        <input type="radio" name="rdDestinaIdemReme" id="rdDestinaIdemRemeS" value="sim"  required> SIM ou
                        <input type="radio" name="rdDestinaIdemReme" id="rdDestinaIdemRemeN"  value="nao" checked> NÃO
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-12" id="DivdoDestinatarioCopiado" hidden>
                  	<div class="col-md-12 col-lg-6">
                  		<p style="font-size: 21px;"> <span  class="fa  fa-check-square fa-2x" style="color: #5cb85c;"></span> Todos os itens Foram Copiados com sucesso!</p>
						<p>Caso queira reverter, apenas selecione a opção <b>"Não"</b> acima.</p>
                  	</div>
                  </div>
                  <div class="col-md-12" id="DivdoDestinatario">
                  <div class="col-md-12 col-lg-6">
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Nome:</label>
                      <div class="col-sm-8">
                        <select name="seldestinatario" id="seldestinatario" class="select-tag form-control" maxlenght="45" style="width: 100%;" onChange="buscadestinatario()" >
                          <option value="">Escolha um Destinatario...</option>
                          <?php
                            $sql_sel_clientes_preparado->execute();
                            while($sql_sel_clientes_dados = $sql_sel_clientes_preparado->fetch()){
                          ?>
                            <option value="<?php echo $sql_sel_clientes_dados['id'];?>"><?php  echo $sql_sel_clientes_dados['nome'];?></option>
                          <?php
                            }
                          ?>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">E-mail:</label>
                      <div class="col-sm-8">
                        <input type="email" id="txtemaild" name="txtemaild" class="form-control" maxlength="70"  placeholder="[email protected]">
                        <div class="help-block with-errors"></div>

                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Telefone Fixo:</label>
                      <div class="col-sm-8">
                        <input type="tel" name="txttelefone1d" id="telefone1d" class="form-control" pattern="^([0-9]{1,20})$" maxlength="20" placeholder="34391090" >
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">Telefone Celular:</label>
                      <div class="col-sm-8">
                        <input type="tel" name="txttelefone2d" id="telefone2d" class="form-control" maxlength="20" pattern="^([0-9]{1,20})$" placeholder="955123231351">
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Estado:</label>
                      <div class="col-sm-8">
                        <select name="selestadod" id="selEstadod" class="select-basic form-control" style="width: 100%;" onChange="mostrarcidaded()" >
                          <option value="">Escolha um Estado..</option>
                          <?php
                            $sql_sel_estados_preparado->execute();
                            while($sql_sel_estados_dados = $sql_sel_estados_preparado->fetch()){
                          ?>
                          <option value="<?php echo $sql_sel_estados_dados['id'];?>"><?php  echo $sql_sel_estados_dados['nome'];?></option>
                          <?php
                            }
                          ?>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                  </div>
                  <div class="col-md-12 col-lg-6">
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Cidade:</label>
                      <div class="col-sm-8">
                        <select name="selcidaded" id="selCidaded" class="select-basic form-control" style="width: 100%;" onchange="mostrarbairrod()" >
                          <option value=''>Escolha um Estado Primeiro...</option>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Bairro:</label>
                      <div class="col-sm-8">
                        <select name="selbairrod" id="selBairrod" class="select-basic form-control" style="width: 100%;" >
                          <option value=''>Escolha uma Cidade Primeiro...</option>
                        </select>
                        <div class="help-block with-errors"></div>
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Logradouro:</label>
                      <div class="col-sm-8">
                        <input type="text" name="txtlogradourod" id="logradourod" class="form-control" placeholder="Marques de Olinda" maxlength="45">
                      </div>
                    </div>
                    <div class="form-group row">
                      <label class="col-sm-4 form-control-label">*Número:</label>
                      <div class="col-sm-8">
                        <input type="text" name="txtnumerod" id="numerod" class="form-control" maxlength="6" placeholder="1222">
                      </div>
                    </div>
                  </div>
                  <!--/. div col-md-12 col-lg-6  -->
                  <div class="form-group row col-lg-12">
                    <label class="col-sm-2 form-control-label">Comentário / Complemento:</label>
                    <div class="col-sm-10">
                      <textarea name="txtacomplementod" id="complementod" class="form-control" placeholder="Comentário/Complemento do endereço do Destinatário..." ></textarea>
                      <div class="help-block with-errors" style="color: rgb(217, 65, 65);">Necessário caso logradouro e número não estejam preenchidos!</div>
                    </div>
                  </div>
              </div>
              </div>
            </div>
          </div>

            <div class="panel panel-default">
              <div class="panel-heading">
                <h4 class="panel-title">
                  Produtos
                </h4>
                <a data-toggle="collapse" data-parent="#accordion" href="#collapseFour">
                <li class="fa fa-minus-square fa-2x navbar-right minimizar"  alt="" width="30px" height="30px"></li>
                </a>
              </div>
              <div id="collapseFour" class="panel-collapse collapse">
                <div class="panel-body">
                  <div class="table-responsive">
                      <table class="table table-striped table-bordered table-hover" cellspacing="0" width="100%"  >
                        <thead>
                          <tr>
                            <th>*Tipo</th>
                            <th>*Categoria</th>
                            <th>*Produto</th>
                            <th>*QTD</th>
                            <th>Descrição</th>
                            <th>*Valor</th>
                            <th><a class="adicionarCampo" title="Adicionar Novo Produto" style="cursor: pointer;"><span class="fa fa-plus-square fa-2x" style="color: #a6ce39 !important;"></span></a></th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr class="linhas" id="id__0">
                            <td>
                              <select name="seltipo[]" id="seltipo[]" onChange="mostraprodutos('tipo', this)" class="select-basic form-control" >
                                <option value=''>Selecione</option>
                                <option value='0'>Padrão</option>
                                <option value='1'>Especial</option>
                                <option value='2'>Kit's</option>
                              </select>
                            </td>
                            <td>
                              <select name="selcategoria[]" id="selcategoria[]" class="select-basic form-control" onChange="mostraprodutos('categoria', this)" style="min-width: 98px !important;">
                                <option value='' selected>Selecione</option>
                                <?php
                                  while($sql_sel_categorias_dados = $sql_sel_categorias_preparado->fetch()){
                                    $id = $sql_sel_categorias_dados['id'];
                    								$nome = $sql_sel_categorias_dados['nome'];

                                    echo "<option value='$id'> $nome </option>";
                                  }
                                ?>
                              </select>
                            </td>
                            <td>
                              <select name='selproduto[]' class="select-basic form-control"  >
                                <option value=''>Selecione...</option>
                              </select>
                            </td>
                            <td>
                              <input type="number" name="txtqtd[]"   class="form-control" min="1" max="" maxlength="4" style="width: 100%;">
                            </td>
                            <td>
                              <input type="text" name="txtdescricao[]"  class="form-control" maxlength="45" style="width: 100%;">
                            </td>
                            <td>
                              <input type="text" name="txtvalor[]" id="txtvalor[]"  class="form-control" maxlength="8" style="width: 100%;">
                            </td>
                            <td><a class="removerCampo" title="Remover Produto" style="cursor: pointer;"><span class="fa fa-minus-square fa-2x"></span></a></td>
                          </tr>
                        </tbody>
                      </table>
                    </div>
                    <div class="form-group row" style="margin-top: 13px;">
                      <label class="col-sm-2 form-control-label">Descrição Geral:</label>
                      <div class="col-sm-10">
                        <textarea name="txtadescricao_geral" class="form-control" placeholder="Descreva uma descrição Geral sobre os Produtos aqui..."></textarea>
                      </div>
                    </div>
                </div>
              </div>
            </div>
          </div>
          <!-- .panel-group -->
        </div>
        <!-- .panel-body -->
      </div>
      <!-- /.panel panel-default-->
    </div>
    <!-- /.col-lg-12 -->
  </div>

  
          <div class="form-group row">
            <div class="col-lg-4 text-right">
              <button type="reset" class="btn btn-danger">Limpar campos</button>
            </div>
            <div class="col-lg-8">
              <button type="submit" class="btn btn-success" >Enviar</button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</form>

  • This is a same click problem, if the user ends up clicking twice on the function before it ends will generate this problem, what you need to do is a check to block this action. remembering that mouse with problem can generate almost instant double click.

  • You can put the code where you submit the request?

  • @Tiagogomes I edited the code but I had to take out small fields because it didn’t fit all the code. It will help me a lot if I get it!! D

1 answer

1


You can put lock on button not to allow double click:

<button type="button" class="btn btn-success" onclick="this.disabled=true;this.form.submit();" >Enviar</button>

and also validate if post is equal to the previous not performing the:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
   $request = implode($_POST);
   if(isset($_SESSION['last_request']) && ($_SESSION['last_request'] == $request)) {
      //$pag = --- página onde deseja redicionar, caso queira ex: index.php ----
      header("Location: ".$pag); 
      exit;
   }else {
      $_SESSION['last_request'] = $request;
   }
}
  • worked however it happens the following error: Notice: Array to string Conversion in C: xampp htdocs pedido sistema admin pedidos ff_ins_pedidos.php on line 19. That would be the line : request = implode($_POST);

  • Oh yes I didn’t look at all your code and I didn’t realize that you have several arrays going through the post then implode gives error because it doesn’t accept array inside array to convert into string, use $request = json_encode($_POST); remembering that you have to have session_start(); on your page

  • Thank you very much!!

Browser other questions tagged

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