当我提交表单时,我得到了正确的 javascript 验证消息,但表单仍然被提交,并且我得到了 php 验证消息。如果通过了 javascript 测试,我如何才能提交我的表单:
形式:
enter code here// first name is mandatory
if (!$firstName) {
$msg_to_user = '<h4 class="errmsg"><font color="FF0000">Please enter your first name.</font></h4>';
// email is mandatory
} else if (!$email) {
$msg_to_user = '<h4 class="errmsg"><font color="FF0000">Please type an email address ' . $firstName . '.</font></h4>';
// check email is valid
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$msg_to_user = '<h4 class="errmsg"><font color="FF0000">' . $email . ' is not a valid email address.</font></h4>';
// check postcode is a number
} else if (!empty($postcode) && !is_numeric($postcode)) {
$msg_to_user = '<h4 class="errmsg"><font color="FF0000">Postcode must be a numeric value.</font></h4>';
// check postcode is greater than 4 chars
} else if (!empty($postcode) && (strlen ($postcode) < 4)) {
$msg_to_user = '<h4 class="errmsg"><font color="FF0000">Postcode must be at least 4 characters.</font></h4>';
// check postcode is less than 12 chars
} else if (strlen ($postcode) > 12) {
$msg_to_user = '<h4 class="errmsg"><font color="FF0000">Postcode must be less than 12 characters.</font></h4>';
// check email doesn't exist
} else if ($numRows > 0) {
$msg_to_user = '<h4 class="errmsg"><font color="FF0000">' . $email . ' is already in the system.</font></h4>';
// if all test passed, insert details into database
} else {
$sql_insert = mysql_query("INSERT INTO newsletter (firstName, lastName, email, postcode, dateTime)
VALUES('$firstName','$lastName','$email','$postcode',now() )") or die(mysql_error());
$subscribed = '<h4 class="submsg"><font color="0066FF">Thanks ' . $firstName . ' ' . $lastName . ', ' . $email . ' has been subscribed to our newsletter.</font></h4>';
?>
<form name="newsletter" method="post" action="<?php echo $_SERVER['PHP_SELF'];" ?>
id="newsletter" onSubmit="postcodeval(); emailval(); notnull();">
<fieldset>
<label for="firstName" id="firstName_label">First Name*:</label>
<input type="text" name="firstName" id="firstName" size="36" value="<?php echo isset($firstName) ? $firstName : '' ?>" class="text-input" onBlur="notnull()" />
<br />
<label for="lastName" id="lastName_label">Last Name:</label>
<input type="text" name="lastName" id="lastName" size="36" value="<?php echo isset($lastName) ? $lastName : '' ?>" class="text-input" />
<br />
<label for="email" id="email_label">Email*:</label>
<input type="text" name="email" id="email" size="36" value="<?php if (isset($_POST['submit_newsletter'])){echo $_SESSION['newsletterSignup'];} ?><?php echo isset($email) ? $email : '' ?>" class="text-input" onBlur="emailval()" />
<br />
<label for="postcode" id="postcode_label">Postcode:</label>
<input type="text" name="postcode" id="postcode" size="12" value="<?php echo isset($postcode) ? $postcode : '' ?>" class="text-input" onBlur="postcodeval()" />
<br />
<input type="submit" name="submit" id="submit_btn" value="Subscribe" />
</fieldset>
Javascript:
//Name cannot be blank
function notnull() {
var z = document.forms["newsletter"]["firstName"].value;
if (z === null || z === "") {
inlineMsg('firstName', 'You must enter your name.', 3);
return false;
}
}
//Vaidate email address
function emailval() {
var y = document.forms["newsletter"]["email"].value;
var atpos = y.indexOf("@");
var dotpos = y.lastIndexOf(".");
if (y === null || y === "") {
inlineMsg('email', 'You must enter an email.', 3);
}
if (y === null || y === "") {
return true;
}
if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= y.length) {
inlineMsg('email', 'Not a valid e-mail address.', 3);
return false;
}
}
//Vaidate postcode
function postcodeval() {
var x = document.forms["newsletter"]["postcode"].value;
if(!!x){
if (isNaN(x)) {
inlineMsg('postcode', 'Must be numbers only.', 3);
}
else if (x.length < 4) {
inlineMsg('postcode', 'Must be more than than 4 characters.', 3);
}
else if (x.length > 12) {
inlineMsg('postcode', 'Must be less than 12 characters.', 3);
}
}else{
}
}