Forum Moderators: open
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?
<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
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.
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
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