Calling form submission by function

Asked

Viewed 705 times

1

I am trying to send a form through a function. It follows the structure.

form with id: nickname
input with id: surname
event keyup in input.

onkeyup="$(this).check()"

Javascript

(function($) {
        $.fn.check = function() {
            alert('a');
             $('#apelido').delay(200).submit();
        
        };
    })(jQuery);

    $("#apelido").submit(function (event) {  

        alert('s');     
        event.preventDefault(); 
        $.ajax({       
        type: "get",
        url: '<?php echo base_url();?>index/check',
        data: {id: '425', inputv : 'adm', nameinput: 'apelido'},
        success: function (response) {
                alert(response);
                      }
        });
    });
   

The first Alert is called, the second not... I no longer know what to do. Any help is welcome.

  • you cannot have more than one object with the same id as the name itself already says id = identification would be impossible to identify someone with two equal fingerprints, the class may be equal but the id does not

2 answers

1


Hello. Apparently you have two identical id’s on the same page, which is invalid by specification, although it refers to different tags.

You can change the form id to something like form-apelido and the input to input-apelido and make the appropriate changes to the jquery selector.

A suggestion:

HTML

<form id="form-apelido">
<input id="input-apelido" onkeyup="$(this).check()" />
</form>

JAVASCRIPT

(function($) {
        $.fn.check = function() {
            alert('a');
             $('#form-apelido').delay(200).submit();

        };
    })(jQuery);

    $("#form-apelido").submit(function (event) {  

        alert('s');     
        event.preventDefault(); 
        $.ajax({       
        type: "get",
        url: '<?php echo base_url();?>index/check',
        data: {id: '425', inputv : 'adm', nameinput: 'apelido'},
        success: function (response) {
                alert(response);
                      }
        });
    });

FIDDLE with code working: https://jsfiddle.net/mrlew/971b2wgx/1/

  • agree with you

  • Okay, the problem wasn’t just the repeated id, I had tried to put a form inside another form, kkk, all this to check the bd to see if the login was repeated, trying to get around the deficiency of the codeigniter’s is_unique. What did I do? I created a ghost form to do the post and the data needed for the check were placed in global variables, so now I can call the function of any field without worrying, I will post the code below.

0

Problem: Bypass the Codeigniter deficiency in the is_unique issue for update, since it does not check if the field to be updated is the same user, thus preventing the use of it in update form.

Solution: Form with user data obtained from BD. I only put the field for search for clarity, but consider several fields such as, tel, cel, zip etc.

<form action="algumlugarproupdate.php" method="post">
<input type="text" id="pesquisa" onkeyup="$(this).check($(this).val())">
</form>

Form "Phantom":

 <form id="test" method="get">
 </form>

Javascript excerpt

<script type="text/javascript">    
  var field1;
  (function($) {
  $.fn.check = function(data1) {
   //Aqui eu passo os valores da função para variáveis globais
   //Fica mais fácil de manipular fora deste escopo
   field1=data1;  
   //Aqui envio o form "fantasma" com delay de 0.2 seg       
 $('#test').delay(200).submit();        
};
})(jQuery);
 //Aqui intercepto o submit
$("#test").submit(function (event) {  
event.preventDefault(); 
$.ajax({       
type: "get",
url: 'retorno_true_false.php',
data: {login: field2},
    //pego o que foi retornado do retorno_true_false.php
    success: function (data) {
    //retiro possíveis espaços
    data.trim();
    if (data>0) {
        alert("Ops, este login já existe");
        //volto o campo pesquisado para o valor original
        $("#pesquisa").val($("#pesquisa").attr("value"));
        }
    }
});
})(jQuery);

I guess that’s it!

Browser other questions tagged

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