2012-04-27 57 views
0
<form name="post" ACTION="http://epansoftware.com/cgi-sys/cgiemail/cgi-bin/cgimailx.txt" accept-charset="UTF-8" METHOD="post"> 
    <fieldset> 
    <legend>OR LEAVE US A MESSAGE HERE</legend> 
    <label for="name" class="fixedwidth">Name</label> 
    <input type="text" Name="name" id="name" value="" size="50"/><br /> 
    <label for="company" class="fixedwidth">Company</label> 
    <input type="text" Name="company" id="company" value="" size="50"/><br /> 
    <label for="email" class="fixedwidth">Email</label> 
    <input type="text" Name="email" id="email" size="50"/><br /> 
    <label for="subject" class="fixedwidth">Subject</label> 
    <select name="subject" id="subject"> 
    <option>Support</option> 
    <option>Sales Inquiry</option> 
    <option>Custom Database</option> 
    </select><br /> 
    <label for="message" class="fixedwidth">Message</label> 
    <textarea type="text" Name="message" id="message" cols="40" rows="20"></textarea> 
    <div class="buttonarea"> 
    <input type="submit" name="submit" value="Submit" onClick="testResults()"> 
    <INPUT TYPE="hidden" NAME="success" VALUE="http://www.epansoftware.com"> 
    </div> 
    </fieldset> 
    </form> 

我有這種形式的HTML,以及這個JavaScript函數定義在頭文件:通過Javascript停止CGI電子郵件腳本?

<script language="javascript"> 
    function testResults() 
    { 
    var name = document.getElementById("name").value; 
    var email = document.getElementById("email").value; 
    var message = document.getElementById("message").value; 
    var fields = new Array(); 
    fields[0] = name; 
    fields[1] = email; 
    fields[2] = message; 
    var tester = /[A-z]|[0-9]/; 
    if(! tester.test(fields[0])) 
    { 
     alert("Please type information in all the required fields!"); 
     return false; 
    } 
    else if(! tester.test(fields[1])) 
    { 
     alert("Please type information in all the required fields!"); 
    } 
    else if(! tester.test(fields[2])) 
    { 
     alert("Please type information in all the required fields!"); 
    } 
    else if(tester.test(fields[0]) && tester.test(fields[1]) && tester.test(fields[2])) 
    { 
     alert("Your message has been sent!"); 
    } 
    else 
    { 
     alert("ERROR 5: Regex Error"); 
    } 
    } 
</script> 

我的問題是,當我調用函數,如果所有的字段沒有填寫,我不想發送電子郵件。但是,現在的方式是,不管發生什麼,都會發送一封電子郵件。有沒有什麼辦法可以阻止CGI腳本(這是動作在表單方法中做的)從運行javascript?

回答

1

return false添加到函數的末尾。在您的<form>標記中添加onSubmit="return testResults()"(刪除您現在如何調用它)。 <form>標記的onSubmit屬性中的return false將阻止表單提交。

+0

onsubmit docs:http://msdn.microsoft.com/en-us/library/ie/ms536972(v=vs.85).aspx – 2012-04-27 23:36:57

+0

沒有與'onSubmit =「一起使用return testResults()」' ,但我用'onClick =「return testResults()得到它」'謝謝 – slinky773 2012-04-27 23:45:46