我只需要帮助完成此验证我在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";
}
答案 0 :(得分: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];
}
}
答案 1 :(得分: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;
}
希望有所帮助。
问候。