Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript - Form always being submitted. Even when it should not to.

Status
Not open for further replies.

SgtGranite

Programmer
Apr 8, 2009
3
BR
Hello there, people!

I'm new to Javascript, and I'm having a very strange problem.
What should happen is that if one of three text fields was blank, when the second image was clicked, an alert should appear, requiring the due data. If all the fields were filled, the code was supposed to submit the form. Now what happens is that whenever any of the images is clicked, they trigger their respective actions, but the form will ALWAYS be submitted. Even if the text fields are blank. Even if the image clicked was the second one, the one for the popup window. WTF is happening?
Please, take a look at my code:

Code:
<script>
  function check(){
    var go = true; 
    if(document.contact.name.value ==""){
    alert("Inform your name");
    document.contact.name.focus();
    go = false;
  }else if(document.contact.phone.value ==""){
    alert("Inform your phone number");
    document.contact.phone.focus();
    go = false;
  }else if(document.contact.pass.value ==""){
    alert("Inform your password");
    document.contact.pass.focus();
    go = false;
  }else if(go == true){
    document.contact.submit();
  }
</script>

<form name="contact" id="contact" method="post" action="admin/acao/client_cadastre.php?acao=inserir">
  <input type="text" name="name" id="name">
  <input type="text" name="phone" id="phone">
  <input type="text" name="pass" id="pass">
  <img type="image" src="images/imageA.jpg" onclick="Javascript: window.open("falecon.php","Falecon","location=1,status=1,scrollbars=1,width=350,height=350");mywindow.moveTo(600,400);" />
  <img type="image" src="images/imageB.jpg" onclick="check()" />
</form>

Any help will be much appreciated.

Thanks in advance.
 
[1]
<img type="image" src="images/imageB.jpg" onclick="check()" />
[tt]<[red]input[/red] type="image" src="images/imageB.jpg" onclick="[red]return [/red]check()" />[/tt]
[2][tt]
function check(){
[red]//[/red]var go = true;
if(document.contact.name.value ==""){
alert("Inform your name");
document.contact.name.focus();
[red]//[/red]go = false;
[blue]return false;[/blue]
}else if(document.contact.phone.value ==""){
alert("Inform your phone number");
document.contact.phone.focus();
[red]//[/red]go = false;
[blue]return false;[/blue]
}else if(document.contact.pass.value ==""){
alert("Inform your password");
document.contact.pass.focus();
[red]//[/red]go = false;
[blue]return false;[/blue]
}else [red]//[/red]if(go == true){
[red]//[/red]document.contact.submit();
//just do nothing would be fine,
//input type image is a submit control
}
[/tt]
 
[2a] The comment out of the last if extended to include the open brace, which should not be. This is the correction to the part. Further, I would rather include return true to it even it is not straightly necessary.
[3] Further more, your function has a closing brace short for the function itself. This is shown together with the above note.
[tt]
function check()[highlight]{[/highlight]
//etc etc the same
}else [red]{[/red] //if(go == true){
//document.contact.submit();
//just do nothing would be fine,
//input type image is a submit control
[blue]return true;[/blue]
}
[highlight]}[/highlight]
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top