Duplication of submit with jquery and php

1

I have a form on which to register phones called edit_form.php:

<form method='post' name="<?php echo $idEmp; ?>" id='emp-SaveForm-tel' action="#">
    <input type='hidden' name='id' value="<?php echo $idEmp; ?>">
    <table class='table table-bordered'>
        <tr>
            <td>Telefone</td>
            <td>
                <input type='text' name='tel' class='form-control' placeholder='Ex : (11) 9XXXX-XXXX / (11) XXXX-XXXX'>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <button type="submit" class="btn btn-primary" name="btn-save" id="btn-save">
                    <span class="glyphicon glyphicon-plus"></span> Salvar Telefone
                </button>  
            </td>
        </tr>

    </table>
</form>

And I have a script that sends this form that is in crudEmpresa.js:

   // JavaScript Document
   $(document).ready(function(){
    /*Cadastro de telefone*/
     $(document).on('submit', '#emp-SaveForm-tel', function() {

   $.post("queryInsertTel.php", $(this).serialize())
    .done(function(data){
        $("#dis").fadeOut();
        $("#dis").fadeIn('slow', function(){
             $("#dis").html('<div class="alert alert-info">'+data+'</div>');
             $("#emp-SaveForm-tel")[0].reset();
             $("body").fadeOut('slow', function()
             {
                $("body").fadeOut('slow');
                window.location.href="index.php";
             });                 
         });    
    });   
    return false;
});
/*FIM Cadastro de telefone*/


/* Cadastro de Empresa*/
var enviandoForm = false;
$(document).on('submit', '#emp-SaveForm', function() {
  if (enviandoForm) {
    return false;
}
this.disabled = true;
enviandoForm = true; 
   $.post("queryInsert.php", $(this).serialize())
    .done(function(data){
        var notification = new NotificationFx({
        wrapper : document.body,
        message : ('<div class="alert alert-info">' +data+ '</div>'),
        layout : 'growl',
        effect : 'scale',
        type : 'notice',
        ttl : 6000,
        onClose : function() { return false; },
        onOpen : function() { return false; }
        });
        $("#emp-SaveForm")[0].reset();
        notification.show();
        setTimeout(function() {
        $("body").fadeOut('slow', function()
             {      

                 $("body").fadeOut('slow');
                 window.location.href="index.php";
         }).always(function() {
                enviandoForm = false; //Libera o form
                this.disabled = false;
          });
          }, 1000); 

     });  
      return false;
});
/*FIM Cadastro de empresa*/











/* Data Delete Starts Here */
$("#btn-del").click(function()
{
    var id = $(this).attr("value");
    var del_id = id;
/*  var parent = $(this).parent("td").parent("tr");*/
    if(confirm('Sure to Delete ID no = ' +del_id))
    {
        $.post('queryDelete.php', {'del_id':del_id}, function(data)
        {
        var notification = new NotificationFx({
        wrapper : document.body,
        message : ('<div class="alert alert-info">' +data+ '</div>'),
        layout : 'growl',
        effect : 'scale',
        type : 'notice',
        ttl : 6000,
        onClose : function() { return false; },
        onOpen : function() { return false; }
        });
        notification.show();
        setTimeout(function() {
        $("body").fadeOut('slow', function()
             {      

                 $("body").fadeOut('slow');
                 window.location.href="index.php";
         }).always(function() {
                enviandoForm = false; //Libera o form
                this.disabled = false;
          });
          }, 1000); 
      });   
    }
    return false;
});
/* Data Delete Ends Here */

$(".btn-del-tel").click(function()
{
    var id = $(this).attr("value"),idEmp = $(this).attr("name");
    var edit_id = idEmp;
    var del_id = id;
/*  var parent = $(this).parent("td").parent("tr");*/
    if(confirm('Sure to Delete ID no = ' +del_id))
    {
        $.post('queryDeleteTel.php', {'del_id':del_id}, function(data)
        {
        var notification = new NotificationFx({
        wrapper : document.body,
        message : ('<div class="alert alert-info">' +data+ '</div>'),
        layout : 'growl',
        effect : 'scale',
        type : 'notice',
        ttl : 6000,
        onClose : function() { return false; },
        onOpen : function() { return false; }
        });
        notification.show();
        setTimeout(function() {
         $(".content-loader").fadeOut('slow', function()
          {
            $(".content-loader").fadeIn('slow');
            $(".content-loader").load('edit_form.php?edit_id='+edit_id);
            $("#btn-add").hide();
            $("#btn-view").show();
          }).always(function() {
                enviandoForm = false; //Libera o form
                this.disabled = false;
          });
         }, 1000); 
      });   



    }
    return false;
});




/* Get Edit ID  */
$(".edit-link").click(function()
{
    var id = $(this).attr("id");
    var edit_id = id;

        $(".content-loader").fadeOut('slow', function()
         {
            $(".content-loader").fadeIn('slow');
            $(".content-loader").load('edit_form.php?edit_id='+edit_id);
            $("#btn-add").hide();
            $("#btn-view").show();
        });

    return false;
});



/* Get Edit ID  */

$("#btn-add-tel").click(function()
{
    var id = $(this).attr("value");
    var addtel_id = id;

        $(".content-loader").fadeOut('slow', function()
         {
            $(".content-loader").fadeIn('slow');
            $(".content-loader").load('add_tel_form.php?idEmp='+addtel_id);
            $("#btn-add").hide();
            $("#btn-view").show();
        });

    return false;
});

$("#btn-add-end").click(function()
{
    var id = $(this).attr("value");
    var addend_id = id;

        $(".content-loader").fadeOut('slow', function()
         {
            $(".content-loader").fadeIn('slow');
            $(".content-loader").load('add_end_form.php?addend_id='+addend_id);
            $("#btn-add").hide();
            $("#btn-view").show();
        });

    return false;
});

/* Update Record  */
$(document).on('submit', '#emp-UpdateForm', function() {

   $.post("queryUpdate.php", $(this).serialize())
    .done(function(data){
        $("#dis").fadeOut();
        $("#dis").fadeIn('slow', function(){
             $("#dis").html('<div class="alert alert-info">'+data+'</div>');
             $("#emp-UpdateForm")[0].reset();
             $("body").fadeOut('slow', function()
             {
                $("body").fadeOut('slow');
                window.location.href="index.php";
             });                 
         });    
    });   
    return false;
});
/* Update Record  */
 });

without mentioning the php that inserts the record in the database, called queryInsertTel.php:

<?php

require_once '../../ Connect / dbconfig.php';

if($_POST)
{
    $id = $_POST['id'];
    $tel = $_POST['tel'];

    $stmtB = $db_con->prepare("INSERT INTO tb_telefones(IdEmpresaTelefone,Telefone)VALUES(:idEmp,:tel)");
    $stmtB->bindParam(":idEmp", $id);
    $stmtB->bindParam(":tel", $tel);
    if($stmtB->execute())
    {
        echo "Successfully Added";
    }
    else{
        echo "Query Problem";
    }   

 }

   ?>

The problem is that you are registering 2 times the phone number, I only click once on save phone and it saves and times

    
asked by anonymous 01.02.2017 / 18:08

1 answer

-1

In the 'if' checkpoint, point to verify the actual variables, not the method pass. I believe what is happening is that by checking $ _post the script checks for two $ _post passages, which is what actually occurs.

So instead of just checking $ _post put it like this

if($_post['id'] && $_post['tel']){
   .... seu código segue normal aqui...
    
01.02.2017 / 18:28