Forum Moderators: open

Message Too Old, No Replies

Function return value not working

         

ffoeg

6:40 am on Feb 10, 2007 (gmt 0)

10+ Year Member



Hey all. I have a specific function that checks the values of a web form before submitting. Mainly because I don't want them to have to wait while the page reloads after PHP picks up the errors.

For some reason, the function does not seem to want to return false. I have called the function in the onsubmit part of the form.

onsubmit = "checkForm(this)"

Here is the code below.


function checkForm(form_name) {
var problem = false;
var msg = new Array(3);
if (form_name.post_name.value == "") {
msg[0] = "Your name has not been filled in.\n";
problem = true;
}
if (form_name.post_email.value == "") {
msg[1] = "Your email address was not filled in.\n";
problem = true;
}
if (form_name.post_msg.value == "") {
msg[2] = "You have not made a comment.";
problem = true;
}
if (problem) {
for (var i=0;i<=2;i++) {
alert (msg[i]);
}
return false;
}
else {
return true;
}
}

It must be getting to the

return false;
bit, because if I put a
document.write ("Blah");
just before it, it does write it. I have checked it against the textbooks that I have, and it seems the same, apart from the different variable names.

So, my question is: Can you see whether there is anything wrong?

daveVk

10:23 am on Feb 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try

form. onsubmit = "return checkForm(this);"

What goes in quotes are statement[s] of an anon function that will not return anything unless explicitly done.

ffoeg

2:54 pm on Feb 10, 2007 (gmt 0)

10+ Year Member



So what you're saying is that in the HTML, I would say:
<form name="templateForm" id="templateForm" method="post" action="" onsubmit="return checkForm(this)">

That's the first line of the opening form tag. If this is what you're saying, I tried it, and it didn't work :(

scriptmasterdel

4:59 pm on Feb 10, 2007 (gmt 0)

10+ Year Member



Could you please post the content of the html form.

I think this may help.

Thank you.

scriptmasterdel

5:23 pm on Feb 10, 2007 (gmt 0)

10+ Year Member



Also, i tried this and it seems to be working AOK?!

<script>

function checkForm(form_name)
{
var problem = false;
var msg = new Array(3);

if (form_name.post_name.value == "")
{
msg[0] = "Your name has not been filled in.\n";
problem = true;
}

if (form_name.post_email.value == "")
{
msg[1] = "Your email address was not filled in.\n";
problem = true;
}

if (form_name.post_msg.value == "")
{
msg[2] = "You have not made a comment.";
problem = true;
}

if (problem)
{
for (var i = 0; i <= 2; i++)
{
alert(msg[i]);
}
return false;
}
else
{
return true;
}
}

</script>

<form onSubmit="return checkForm(this);">
name <br /><input name="post_name"><br /><br />
e-mail <br /><input name="post_email"><br /><br />
message <br /><input name="post_msg"><br /><br /><br />
<input type="submit" name="go" value=" send "><br /><br />
</form>

Maybe something else in the document is interfering?

Hope this helps a little.

Del

ffoeg

5:50 pm on Feb 10, 2007 (gmt 0)

10+ Year Member



Thanks, Del.

That is incredibly weird. There has to be something else interfering with the script. I copied yours straight off the page and tested it, and it works fine.

I then went through my code, and compared it to yours - it is the same tag for tag, word for word, yet it doesn't work properly. I'll have to take another a look at it.

But here is the full form code anyways:

<form name="templateForm" id="templateForm" method="post" action="" onSubmit="return checkForm(this);">
<div>
<p>
<span>Your name:</span>
<input type="text" name="post_name" value="<?php echo stripslashes($post_name);?>" />
</p>
<p>
<span>Your email:</span>
<input type="text" name="post_email" value="<?php echo $post_email;?>" />
</p>
<p>
<span>Show email?</span>
<br />
<input name="show_email" value="1" type="radio" checked="checked" class="submit" /> Yes
<br />
<input name="show_email" value="0" type="radio" class="submit" /> No
</p>
<p>
<span>Comment:</span>
<textarea name="post_msg" rows="7" wrap="physical"><?php echo stripslashes($post_msg);?></textarea>
</p>
<p class="submit">
<input type="hidden" name="proceed" value="proceed" />
<input type="image" src="images/icon_submit.gif" width="22" height="27" alt="Submit" title="Submit" class="submit" />
</p>
</div>
</form>

And the script code is the exact same as yours.

scriptmasterdel

7:08 pm on Feb 10, 2007 (gmt 0)

10+ Year Member



Ah i see now, because you are using an image instead of a submit button to process the form it's not being recognized when you click the image.

The solution is to add the following to the "image" button.

<input onClick="return checkForm(this.form);" type="image" src="images/icon_submit.gif" width="22" height="27" alt="Submit" title="Submit" class="submit" />

This will only let the button return as true if the specific fields are correctly filled.

It's a pain really because the image button does almost the same process as the submit button but is not classed as one.

I hope i have helped.

Del

ffoeg

11:10 pm on Feb 10, 2007 (gmt 0)

10+ Year Member



Ah, Del, you are legend!

I finally got it working. I added the code to the submit button, but it still didn't work. Then I realised that in messing around with the code just now I had changed both return values to true. So it was always sending.

I've changed it and it now works properly. Thanks sooooo much for the patience you showed in sticking it out with me. I really do appreciate it! :)

I must admit though, it is a bit of a pain that the image button isn't recognized as a proper submit button. :( Oh well, it works and thats what matters.

Once again, thanks so much!

*g

scriptmasterdel

1:42 am on Feb 11, 2007 (gmt 0)

10+ Year Member



Glad i could help, it's no problem at all =)

Thank you
Del