在我的下面的代码中,我正在显示javascript验证。但是,当我点击moduleSubmit
按钮时,它在if (isset($_POST['moduleSubmit'])) {
之后不会显示任何内容。我的问题是,当我点击按钮,并且javascript验证已通过时,如何在提交后显示表单?
function validation() {
var isDataValid = true;
var courseTextO = document.getElementById("coursesDrop");
var moduleTextO = document.getElementById("modulesDrop");
var errModuleMsgO = document.getElementById("moduleAlert");
if (courseTextO.value == "") {
$('#targetdiv').hide();
$('#assessmentForm').hide();
$('#updateForm').hide();
$('#submitupdatebtn').hide();
errModuleMsgO.innerHTML = "Please Select a Course";
isDataValid = false;
} else {
errModuleMsgO.innerHTML = "";
}
if(isDataValid){
$("#myForm").submit();
}
}
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$sql = "SELECT CourseId, CourseNo, CourseName FROM Course ORDER BY CourseId";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->execute();
$sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);
$courses = array(); // easier if you don't use generic names for data
$courseHTML = "";
$courseHTML .= '<select name="courses" id="coursesDrop">' . PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>' . PHP_EOL;
$outputcourse = "";
while($sqlstmt->fetch())
{
$course = $dbCourseId;
$courseno = $dbCourseNo;
$coursename = $dbCourseName;
$courseHTML .= "<option value='" . $course . "'>" . $courseno . " - " . $coursename . "</option>" . PHP_EOL;
if (isset($_POST['courses']) && ($_POST['courses'] == $course)) {
$outputcourse = "<p><strong>Course:</strong> " . $courseno . " - " . $coursename . "</p>";
}
}
$courseHTML .= '</select>';
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?></th>
</tr>
</table>
<p>
<input id="moduleSubmit" type="submit" value="Submit Course and Module" name="moduleSubmit" />
</p>
<div id="moduleAlert"></div>
<div id="targetdiv"></div>
</form>
<?php
if (isset($_POST['moduleSubmit'])) {
$sessionquery = "
SELECT SessionId, SessionName, SessionDate, SessionTime, CourseId, SessionActive
FROM Session
WHERE (CourseId = ? AND SessionActive = ?)
ORDER BY SessionName
";
$active = 1;
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("si",$course, $active);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionName,$dbSessionDate,$dbSessionTime, $dbCourseId, $dbSessionActive);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
if($sessionnum == 0) {
echo "<p><span style='color: red'>Sorry, You have No Assessments under this Module</span></p>";
}
else
{
echo "";
}
...
}
答案 0 :(得分:0)
您必须使用html格式的重定向操作。这会将您重定向到您想要的任何位置.. 等,
if $valid=$form->validate();
{
$this->redirect('form url')
}
答案 1 :(得分:0)
我担心输入type=button
的值不会以表格形式发布。您可以使用Firefox的firebug插件或chrome中的Developer工具来检查http请求中的发布值。或者您也可以print_r($_POST)
查看在php端发布的值。
尝试使用隐藏的输入<input type="hidden" value="1" name="issubmit"/>
来检查它是否是帖子。