I can’t find my mistake

Asked

Viewed 76 times

2

This code works at the top of my site (menu), and I’m trying to use it also in the footer, since I have the registration tab both at the top and in the footer, but in the footer it doesn’t work.

Can anyone help me find the bug? or is it simply impossible to use the same code at the top and footer? Remembering that I am creating all pages separately (footer is a page, menu is another, and joining them with php. For this reason, I believe, that it should not give error, since, supposedly, are not in direct contact one page with the other.

Can anyone help me in what I should do to make it work on both pages? I have to change or add something?

function valida_campos() {
  if (document.getElementById('name').value == '') {
    alert('The field name is obrigatory.');
    document.getElementById('name').focus();
    return false;
  }
  if (document.getElementById('lastname').value == '') {
    alert('The field lastname is obrigatory.');
    document.getElementById('lastname').focus();
    return false;
  }
  if (document.getElementById('birthday').value == '') {
    alert('Field birthday is obrigatory.');
    document.getElementById('birthday').focus();
    return false;
  }
  if (document.getElementById('email').value == '') {
    alert('The field email obrigatory.');
    document.getElementById('email').focus();
    return false;
  }
  if (document.getElementById('password').value == '' || document.getElementById('password').value != document.getElementById('password2').value) {
    alert('Password didnt mach, please try again.');
    document.getElementById('password').focus();
    return false;
  }
}
<form class="default-form" action="cadastrar.php" method="post" onSubmit="return valida_campos();">


  <p class="form-row">
    <input name="name" id="name" type="text" placeholder="Name">
  </p>
  <p class="form-row">
    <input name="lastname" id="lastname" type="text" placeholder="Last Name">
  </p>
  <p class="form-row">
    <input name="birthday" id="birthday" type="text" placeholder="birthday d/m/y">
  </p>
  <p class="form-row">
    <input name="email" id="email" type="text" placeholder="Email">
  </p>
  <p class="form-row">
    <input name="password" id="password" type="password" placeholder="Password">
  </p>
  <p class="form-row">
    <input name="password2" id="password2" type="password" placeholder="Repeat Password">
  </p>

  <input type="submit" placeholder="cadastrar">

</form>

  • Mikaela, I gave an Edit in the question. The error you have is present in the question now edited/formatted?

  • 1

    I gave Ubmit by jsfiddle and was, are you sure you are in error?

  • I also tested, when the fields are filled it correctly identifies. Question: no onSubmit need the return?

  • I think it is indifferent the existence of this Return

  • 1

    @Laérciolopes, @Wesleynascimento, this return is important.

  • It could be another script on your site, because I also tested it here and it’s working normally.

  • so, I also did the test, I’m using this script at the top (menu) and at the bottom of my site, at the top works perfectly, but in the footer is giving error, error in all input connected with javascipt...

  • What’s the mistake? Still hasn’t spoken.

  • that of using the code at the top and footer ... tab of registration both at the top and at the footer .... are not in direct contact one page with the other... It’s kind of confusing!! Better post everything on the question.

  • What do you have at the top and footer? the same form to work with the script?

  • She only described what she is doing and that is giving error, but did not say what is the error (form does not send, javascript does not answer, blank form etc)... From what I understand, maybe she is using the same form at the top and footer, then Javascript will only take the top form, even using the footer.

  • 1

    It must tah generating form duplicity. In this case, you will have two identical forms on the same page, and Javascript will only consider the first one. The solution is to create a mechanism to differentiate one form from another, or else (for me the best form) is to use the form dynamically, pulling the HTML form according to the place where it was called (top or footer).

  • so, I have 3 pages, the index, the footer and the top, all in php, the index, I include the whole and the footer, when I run the script at the top, all right, all right, when I run the script in the footer, it keeps showing that the fields are empty, all of them, that’s my question, Even though I’m on separate pages, I can’t use the same form? With the same javascript script?

  • 1

    The problem is not in the script itself. By putting the form at the top and footer, you are placing the same form 2 times on the same page, and Javascript will recognize only the first. So when you fill in the form in the footer, Javascript will check the one at the top that is blank.

  • Put in the question the code that you make the form appear at the top and then we can find an easy solution to this. Or it already appears on the page load without needing to click anything?

  • Guys, I really appreciate the help, I managed to tidy up, I changed the ID of the form fields and footer javascript and now it’s working 100%, =DDD

  • This actually works, but it is not the best practice to have two forms practically repeated. When you want to make some change in one, you have to do both of us. But if it’s working, success!

  • I am struggling but learning hahaha, I still have to figure out how to put the 2 in one without giving problems, but I confess that even having more work, I am happy to have managed hehehe

Show 13 more comments

1 answer

0

One way to leave the two forms repeated and the same Javascript that does the validation is to identify which form is being used (top or bottom) and validate only this one (ignoring the other) is using the code below (requires jQuery):

Put in the <form> the parameter this in the onsubmit that will serve to identify the form, this way:

onSubmit="return valida_campos(this);"

The advantage of using the this is that it will make your code much leaner and easy to work, and decrease the bytes of your page, improving performance.

Add the variable i in the function that will receive this parameter:

function valida_campos(i) {....

At the beginning of the function, add the code:

for(x=0;x<=$(".default-form").length;x++){
    if($(i).index() != x){
        formulario = $(".default-form:eq("+x+")").detach();
    }
}

This code will identify which of the two forms was used and "highlight" the other, making it outside the scope of Javascript.

Your complete code would look like this:

HTML

<form class="default-form" action="cadastrar.php" method="post" onSubmit="return valida_campos(this);">
  <p class="form-row">
    <input name="name" id="name" type="text" placeholder="Name">
  </p>
  <p class="form-row">
    <input name="lastname" id="lastname" type="text" placeholder="Last Name">
  </p>
  <p class="form-row">
    <input name="birthday" id="birthday" type="text" placeholder="birthday d/m/y">
  </p>
  <p class="form-row">
    <input name="email" id="email" type="text" placeholder="Email">
  </p>
  <p class="form-row">
    <input name="password" id="password" type="password" placeholder="Password">
  </p>
  <p class="form-row">
    <input name="password2" id="password2" type="password" placeholder="Repeat Password">
  </p>

  <input type="submit" placeholder="cadastrar" value="ok">
</form>

Javascript

function valida_campos(i) {

    for(x=0;x<=$(".default-form").length;x++){
        if($(i).index() != x){
            formulario = $(".default-form:eq("+x+")").detach();
        }
    }

  if (i.name.value == '') {
    alert('The field name is obrigatory.');
    i.name.focus();
    return false;
  }
  if (i.lastname.value == '') {
    alert('The field lastname is obrigatory.');
    i.lastname.focus();
    return false;
  }
  if (i.birthday.value == '') {
    alert('Field birthday is obrigatory.');
    i.birthday.focus();
    return false;
  }
  if (i.email.value == '') {
    alert('The field email obrigatory.');
    i.email.focus();
    return false;
  }
  if (i.password.value == '' || i.password.value != i.password2.value) {
    alert('Password didnt mach, please try again.');
    i.password.focus();
    return false;
  }
}

Browser other questions tagged

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