我无法让客户端和服务器端验证工作

时间:2016-08-08 07:24:11

标签: javascript php jquery

我有以下表格

<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"
        }
    }
?>

我不知道为什么服务器端验证不起作用。请帮帮我。

2 个答案:

答案 0 :(得分:1)

<form name = "courses" method="post" action="validation.php">
 // Your form fields.
</form>

您应该在php中指定<form>文件的方法和名称,您的控件将在submit中继续使用。

请参阅此http://www.w3schools.com/php/php_forms.asp

答案 1 :(得分:0)

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处理