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?
– Sergio
I gave Ubmit by jsfiddle and was, are you sure you are in error?
– Wesley Nascimento
I also tested, when the fields are filled it correctly identifies. Question: no
onSubmit
need thereturn
?– Laércio Lopes
I think it is indifferent the existence of this Return
– Wesley Nascimento
@Laérciolopes, @Wesleynascimento, this
return
is important.– Sergio
It could be another script on your site, because I also tested it here and it’s working normally.
– Wictor Chaves
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...
– Mikaela Kleinkauf
What’s the mistake? Still hasn’t spoken.
– Sam
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.
– user60252
What do you have at the top and footer? the same form to work with the script?
– user60252
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.
– Sam
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).
– Sam
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?
– Mikaela Kleinkauf
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.
– Sam
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?
– Sam
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
– Mikaela Kleinkauf
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!
– Sam
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
– Mikaela Kleinkauf