Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

craigdawg

macrumors 6502
Original poster
OK I'm not well-versed in Javascript and was trying to combine a form that I downloaded that doublechecks entries on certain fields my original one that does not and I've run into a roadblock.

There's a conflict with the action of the original button:

Code:
<input type="submit" value="Send" name="submit">
And that of the "new" button:

Code:
<input type="button" value="Submit" name="SB" onClick="sendOff();">
I tried changing the type value to "submit" and that didn't work. The form was sent without the field values being checked by the sendOff routine.

Now I was wondering if it's possible to include a line in the routine that would submit the form values? I specify where to send the viewer (in this case a webpage) here:

Code:
function sendOff(){
   nmcheck = document.TheForm.name.value    
   if (nmcheck.length <1) {
      alert('Please enter your name.')
      return
   }         
   good = false
   checkEmailAddress(document.TheForm.email)
   if ((document.TheForm.email.value ==
        document.TheForm.emx.value)&&(good)){
      window.location= "http://www.pbrg.com/article_index.html"
   }     
   if ((document.TheForm.email.value !=
          document.TheForm.emx.value)&&(good)){
          alert('Both e-mail address entries must match.')
   }
I have very little idea what I'm doing and sure would appreciate some help. 🙂
 
Change it to

I think you need to change it to

<input type="submit" ...

Input type="button' will not submit the form.

H
 
hiltmon said:
I think you need to change it to

<input type="submit" ...

Input type="button' will not submit the form.

H
I already tried that. The form is sent but the contents are not verified.
 
try this:
Code:
<form onSubmit="return sendOff();">
(of course, include in that form tag whatever attributes your form is already using: name="whatever" action="whatever" method="POST" and so on)
and then rewrite your function along these lines:
Code:
function sendOff(){
     if(conditions are not met){
          alert('You screwed up');
          return false;
     }
     else
     {
           return true;
     }
}

this way, if your form fails to validate, the function will return a value FALSE and the form will not be submitted.
 
sonofslim said:
try this:
Code:
<form onSubmit="return sendOff();">
(of course, include in that form tag whatever attributes your form is already using: name="whatever" action="whatever" method="POST" and so on)
and then rewrite your function along these lines:
Code:
function sendOff(){
     if(conditions are not met){
          alert('You screwed up');
          return false;
     }
     else
     {
           return true;
     }
}

this way, if your form fails to validate, the function will return a value FALSE and the form will not be submitted.
Thanks! I'll give that a whirl and report back.
 
oh, and as hiltmon pointed out, make sure your submit button is actually <input type="submit"> or the function won't be called.
 
sonofslim said:
oh, and as hiltmon pointed out, make sure your submit button is actually <input type="submit"> or the function won't be called.

If you really feel the need to use type="button" just make sure you have the following line in your sendOff() function.

document.theForm.submit();

With that said, the form onSubmit() method is a better way to implement validation. In this case you would need either a submit button or a call to the command I mentioned above.


Finally, there is some logic problems in your code. The variable good is set to false, which would mean that neither of your if statements would ever be true. Is that intentional? Also, if the first if (following the name check) evaluates to true, then your form would never be submitted as you seem to be doing a window redirect.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.