Duplicating Submit with jquery and php

Asked

Viewed 64 times

1

I have a form in which 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 found in the 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  */
 });

not to mention the php that inserts the record into 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 it is registering 2 times the phone number, I only click once on save phone and it saves and times

  • I tried to play your scenario in my environment and is only sending a request as expected. What is the file edit_form.php?

  • is an edition page of the company, on the same page has a button called add phone so I load the page add_tel_form

  • Both files are loaded into div.content-loader? When registering a new phone, the file is loaded edit_form.php and there is the button to return to the registration page? That’s it?

  • Yes, exactly, is the registration page is loaded edit_form.php? edit_id=2, then appears the new phone I registered!

  • You can replace the code inside $.post.done() by just one console.log(data) to make sure you are sending two requests?

  • guy couldn’t get!

  • Thus: http://pastebin.com/dP2ZexQx. Return of PHP will appear in the browser console. If there are actually 2 requests, the duplicate return will appear.

Show 3 more comments

1 answer

-1

In the 'if' check point to check the real variables and not the passage of the method. I think what’s happening is that by checking $_post the script checks two $_post passes, which is what actually happens.

So instead of checking only $_post put like this

if($_post['id'] && $_post['tel']){
   .... seu código segue normal aqui...
  • didn’t work out! 2 times!

Browser other questions tagged

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