PDA

View Full Version : Help w/ a script!




craigdawg
Jun 7, 2004, 06:15 PM
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:

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

<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:

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. :)



hiltmon
Jun 8, 2004, 12:25 PM
I think you need to change it to

<input type="submit" ...

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

H

craigdawg
Jun 8, 2004, 03:21 PM
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.

sonofslim
Jun 8, 2004, 03:51 PM
try this:
<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:
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.

craigdawg
Jun 9, 2004, 09:40 AM
try this:
<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:
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.

sonofslim
Jun 9, 2004, 10:49 AM
oh, and as hiltmon pointed out, make sure your submit button is actually <input type="submit"> or the function won't be called.

jeremy.king
Jun 9, 2004, 12:49 PM
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.