我有以下形式的
<form name="courses">
<div="requiredfield">
<input type="text" name="coursename" id="coursename">
</div>
<input type="submit" name="submitform1">
</form>
<form name="students">
<div="requiredfield">
<input type="text" name="studentfirstname" id="studentfirstname">
</div>
<div="requiredfield">
<input type="text" name="studentlastname" id="studentlastname">
</div>
<div="requiredfield">
<input type="text" name="studentage" id="studentage">
</div>
<input type="submit" name="submitform2">
</form>
我使用此代码进行客户端验证
// this works fine
$('#courses').submit(e) {
e.preventDefault();
if ($('#coursename').val() == 0) {
// error message is appended to the div containing the field
// the same for the other form and its field
}
}
不起作用的是服务器端验证
<?php
if (isset($_POST['submitform1'])) {
if (empty($_POST['coursename'])) {
$course_mistake = "please fill this field"
}
}
?>
我不知道为什么服务器端验证不起作用。请帮帮我。
<form name = "courses" method="post" action="validation.php">
// Your form fields.
</form>
您应该在<form>
中指定php
文件的方法和名称,其中控件将在submit
上运行。
看看这个http://www.w3schools.com/php/php_forms.asp
1您阻止表单提交:
$('#courses').submit(e) {
e.preventDefault(); // This prevents form submitting to PHP side
如果你不使用Ajax(我看不到任何相关的代码(,那么你就要注意将数据发送到PHP
2<form name="courses">
表示您使用的是$_GET
方法,但在PHP中您需要$_POST
添加method="post"
或在PHP中使用$_GET
/$_REQUEST
。
3您不会在任何地方输出错误-缺少$course_mistake
处理