Jump to content

Not understanding why my script does not work


stockton

Recommended Posts

I have a web page at http://www.watersprite.co.za/email.shtml which includes javascript to check that the user has filled all fields and only if all fields are completed call the mail.php script to send the message. It always calls the mail.php script even if the alert has appeared to show there is an error.

Please tell me what my silly mistake is(it must be silly as I have this working in other places).

<html>
<head>
<title> WaterSprite's E-Mail Page </title>
<SCRIPT LANGUAGE="JavaScript">
function reset()
{
document.ws_email.name.value="";
document.ws_email.email.value="";
document.ws_email.subject.value="";
document.ws_email.content.value="";
}

function submit_click()
{
var bError = false;
var strErrorMsg = "";
if (ws_email.name.value == "")
    {
    strErrorMsg += "You must enter a name\n";
    bError = true;
    }
else
if (ws_email.email.value == "")
    {
    strErrorMsg += "You must enter an email address\n";
    bError = true;
    }
else
if (ws_email.subject.value == "")
    {
    strErrorMsg += "Please enter the subject\n";
    bError = true;
    }
else
if (ws_email.content.value == "")
    {
    strErrorMsg += "We cannot help you unless you tell us what help you need\n";
    bError = true;
    }

if (bError == true)
    {
    alert(strErrorMsg);
    }
    else
    {
    ws_email.submit();
    }
}
</script>

</head>
<BODY  OnLoad="reset()">
<center><img src="assets/images/watersprites_logo.jpg" alt="WaterSprites email" /></center>
<br />
<H1 ALIGN=center>Send E-mail to WaterSprite</H1>
<pre>
<form name="ws_email" ID="ws_email" action=mail.php method=post>

Your Full Name     :     <input type="text" name="name">
Your EMail address :     <input type="text" name="email">
Subject            :     <input type="text" name="subject">
Your EMail message :     <textarea rows=6 cols=65 name="content" ></textarea>
<center>
<input type="submit" value="Submit" onclick="submit_click()">     <input type="reset" value="Clear">
</center>
</form>
</pre>
<hr width=50%>
<P ALIGN=RIGHT>Page last revised on <!--#echo var="LAST_MODIFIED"-->
</body>
</html>

Link to comment
Share on other sites

I'm having the same problem.  I borrowed some of the function you have here to validate my form.  My idea was to have an action listener that only show the submit button when all required fields are filled.  That probably doesn't answer your question but I thought you might like to hear the idea.

Link to comment
Share on other sites

Thank you for your suggestion but I have done as you suggest by adding a return false but it still fails.

That line now looks like:-

<input type="submit" value="Submit" onclick="submit_click()"; return false;>

Link to comment
Share on other sites

I think that maybe I should read a bit more carefully and see where your quotes started and finished.

<input type="submit" value="Submit" onclick="submit_click(); return false;">

rather than

<input type="submit" value="Submit" onclick="submit_click()"; return false;>

Thanks for the help xenophobia.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.