JS:
The JS for client side validation of email field
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"> </script>
<script type="text/javascript" src="src/js/validate.js"> </script>
<script type="text/javascript">
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); }
});
$().ready(function() {
// validate alphaRegister form on keyup and submit
$("#alphaRegister").validate({
rules: {
email: {
required: true,
email: true
}
},
messages: {
email: "*"
}
});
});
</script>
HTML
The form
<form id="alphaRegister" action="src/php/newSubscriber.php" method="post">
<input type="email" name="email" id="cemail" value="" class="required" />
<div style="float:right; margin:0 5px 2px 0;"><input type="submit" id="submit" name="submit" value="" /></div>
</form>
When I enter a validname@adomain.com, it just says submitted!!
PHP File
The file handling the input of the html
<?php
$host="localhost"; // Host name
$username="myuser"; // Mysql username
$username2="root"; #localhost
$password="mypass"; // Mysql password
$password2=""; #localhost
$db_name="mydbname"; // Database name
$tbl_name="mydbtblname"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username2", "$password2")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot connect to database");
/* Obliterate bad input */
$goodEmail = mysql_real_escape_string($_POST['email']);
$sql= "INSERT INTO alphaSubscribers (alphaSEmAddr) VALUES('$goodEmail')";
$result = mysql_query($sql);
if (!$result) {
die('Invalid Email, please retry.');
}
else {
header( 'Location: /index.php' ) ;
echo "<meta HTTP-EQUIV='REFRESH' content='0; url=/index.php'>";
}
?>
Anyone see what Im doing wrong?
You are sending the form via
GETbut test for thePOSTparameter. Change your form tag toAlso remove the javascript
submitHandler, otherwise the form might not get submitted at all: