我有以下表格
<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>
您应该指定 php
的方法和名称文件在 <form>
,您的控件将继续 submit
.
关于javascript - 我无法让客户端和服务器端验证工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38824227/