Calendar news - Travel and Leisure Articles - WoW Gold - IKA Lab Equipment - Find jobs

PDA

View Full Version : form requirement and halting issue


stauf
Jun 19th 2007, 12:57 pm
Hi, I'm one of todays new guys here for JS and PHP. I don't know either that well but I'm tryin to learn. I used to work with Delphi so I know flow and such.

My issue. I have a form that gets processed to a php page as an order form. What I need is a way to require one name field be filled in, and unless it is, the form will not continue to parse to the php page. Basically I need to trap the user until he/she provides their name. What I found/have now seems to know that the field is blank, but it doesn't trap the user, it simply continues to get parsed.


<script language="JavaScript" type="text/javascript">
<!-- Begin
function verify() {
var themessage = "You are required to complete the following fields: ";
if (document.order.nick.value=="") {
themessage = themessage + " - Handle";
}
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields: ") {
document.order.submit();
}
else {
alert(themessage);
return false;
}
}
// End -->
</script>


// Then in the form

<form action="order.php" method="post" name="order" onSubmit="verify();">

// I've also tried the OnClick handler on the Sumbit buton, which yeilds the same results.


Any suggestions? Thanks!

Mike H.
Jun 19th 2007, 1:16 pm
Use "return" with onsubmit, and do not use .submit()

<form action="order.php" method="post" name="order" onSubmit="return verify(this);">

function verify(nForm){

if (nForm['nick'].value == "")
{
alert('Please complete the name field');
return false;
}
return true;
}

stauf
Jun 19th 2007, 1:42 pm
Thank You Mike!

I can't believe how simple you made it. And how similar it looks to 'back in the day with delphi'...

I hate getting old, I've forgotten more then I care to remember.. lol

Thanks again!

Mike H.
Jun 19th 2007, 1:52 pm
You're welcome. Talk about old, I'll be 52 in September. And yes, I don't like it one bit.

stauf
Jun 19th 2007, 4:05 pm
Aw 52 ain't old.. If you said you were 70, then I'd say you were gettin there.. I'm not there yet, but if I'm this bad currently, I dunno if I wanna know about the future.. lol

Now if I may, one question. How/Where would I add additional fields to be checked? Is there an OR operand that can be used like: ?

if (nForm['nick'].value == "") or (nForm['mod'].value == "") .. or...

Or should I just create a new section like the first for the new field? I do like simple, but have learned to deal with typing over the years.. lol

Thanks again

Mike H.
Jun 20th 2007, 3:49 am
Yes, exactly, a logical OR.
GENERALLY no parens are needed around each item:

if (nForm['nick'].value == "" || nForm['other'].value == "" || nForm['again'].value == "")
{
alert('Please complete ALL fields.);
return false;
}
return true;

The logical AND is: &&

See also, this recent post:
http://forums.digitalpoint.com/showthread.php?p=3510862#post3510862

stauf
Jun 21st 2007, 8:57 pm
Thanks again Mike! :cool: