JS:
用于email
字段
<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 表格
<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>
当我输入validname@adomain.com时,它只是说submitted!!
PHP文件 处理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'>";
}
?>
任何人都能看到我做错了什么?
答案 0 :(得分:3)
您要通过GET
发送表单,但要测试POST
参数。将表单标记更改为
<form id="alphaRegister" action="src/php/newSubscriber.php" method="post">
同时删除javascript submitHandler
,否则表单可能根本无法提交:
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); } // <= remove that line
});
答案 1 :(得分:0)
您的表单正在使用GET
方法,而您的后端PHP代码正在将其处理为$_POST
您的HTML方法应为method="POST">
答案 2 :(得分:0)
来自jquery docs:使用submitHandler实现您自己的表单提交,例如。通过Ajax。
您的提交处理程序验证程序会阻止以常规方式提交该文件。
删除此代码,然后重试:
$.validator.setDefaults({
submitHandler: function() { alert("submitted!"); }
});