1

我只需要完成此验证的帮助,我有 php 检查用户提交表单时是否有任何错误的地方:

$errors = array();


      if (!$getcourseid){
          $errors[] = "You must enter in Course's ID";
  }else if (!$getcoursename){
      $errors[] = "You must enter in Course's Name";
  }else if (!$getduration){
          $errors[] = "You must select Course's Duration";
      }     

      if(!$errors) {

     $insertsql = "
    INSERT INTO Course
        (CourseNo, CourseName, Duration)
      VALUES
        (?, ?, ?)
    ";
    if (!$insert = $mysqli->prepare($insertsql)) {
      // Handle errors with prepare operation here
    }                                           

    $insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);

    $insert->execute();

    if ($insert->errno) {
      // Handle query error here
    }

    $insert->close();

     // don't use $mysqli->prepare here
$query = "SELECT CourseNo FROM Course WHERE CourseNo = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getcourseid);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();  

          }

$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td></td>
  <td id='errormsg'>$errormsg</td>
  </tr>
  <tr>
  <td>Course ID:</td>
  <td><input type='text' name='courseid' value='$getcourseid' /></td>
  </tr>
  <tr>
  <td>Course Name:</td>
  <td><input type='text' id='nameofcourse' name='coursename' value='$getcoursename' /></td>
  </tr>
  <tr>
  <td>Duration (Years):</td>
  <td>{$durationHTML}</td>
  </tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Create Course' name='createbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;

我的问题是,如果没有错误并且数据库内容已成功完成,我想显示一条成功消息,或者如果表单已完成且没有错误但数据库内容无法完成,则显示一条错误消息。我还想向$errormsg变量显示所有消息?我能问一下怎么做吗?

以下是我想要实现的目标,但不确定它是否适用于上面的代码:

               if ($numrows == 1){

               $errormsg = "<span style='color: green'>Course " . $getcourseid .  " - "  . $getcoursename . " has been Created</span>";
               $getcourseid = "";
               $getcoursename = "";
               $getduration = "";

           }else{

               $errormsg = "An error has occured, Course has not been Created";

           }
4

2 回答 2

1

逻辑是这样的,假设你有一系列错误,你可以这样做

if(empty($errormsg)) {
   //Go Ahead
} elseif(!empty($errormsg)) {
  //if(isset($errormsg['index'])) {
     echo 'Error'; 
  }
}

详细说明

假设您有 3 个错误,例如

$errors[0] = 'Username is invalid';
$errors[1] = 'Password is invalid';
$errors[2] = 'Third error';

if(empty($errors)) {
   //Go Ahead
} else {
    if(isset($errors[0])) {
       echo $errors[0]; 
    } elseif (isset($errors[1])) {
       echo $errors[1];
    } elseif (isset($errors[1])) {
       echo $errors[1]; 
    }
}
于 2012-12-08T15:58:49.097 回答
1

尝试使用 jQuery:

在 test.php 中

...
$data['status'] = 1; // Status of the operation (1: Ok, 0: Error)
$data['msg'] = $msg; // Error message or success
header('Content-Type: application/json');
echo json_encode($data);

在 test.js 中:

$("#send").click(function () {
    $.post('test.php', $('#yourForm').serialize(), function(data){
        if(data.status == 1){
            $('#result').show().addClass('success').html(data.msg);
        }
        else{
            $('#result').show().addClass('error').html(data.msg);
        }
    }, "json");
    return false;
});

在 test.html 中:

<form method="post" id="yourForm">
...
<input id="send" type="button"/>
<span id="result"></span>
</form>

在 test.css 中:

.success{
    color: blue;
}
.error{
    color: red;
}

希望能有所帮助。

问候。

于 2012-12-08T16:26:31.927 回答