我的视图中有一个文件(_gen.php),它将选定的数据发送到控制器文件进行验证:
$('#validate').on('click',function(){
var data = []; // data container
// collect all the checked checkboxes and their associated attributes
$("table#subsection_table input[type='checkbox']:checked").each(function(){
data.push({
section : $(this).data('sectionid'),
subsection : $(this).val(),
year : $(this).data('year')
})
});
// JSON it so that it can be passed via Ajax call to a php page
var data = JSON.stringify(data);
$.ajax({
url : "<?php echo Yii::app()->createAbsoluteUrl("scheduler/ScheduleValidation"); ?>",
type: "POST",
data : "myData=" + data,
success : function(data)
{
$("#ajax-results").html(data);
$("#ajax-results").dialog({ width: 500, height: 500})
},
error: function()
{
alert("there was an error")
}
})
console.log(JSON.stringify(data));
$('#dialog').html(data).dialog({ width: 500, heigh: 500});
});
现在#ajax-result是我的按钮之后的一个div标签的id(页面上显示的最后一个东西)。
至于控制器功能,我知道它正确处理数据和正确调用sql(我确定了它)。但是,当我调用renderPartial时,它将正确调用我的_ajax.php文件,但它只会在警告框中显示,而不是#ajax-result标记。控制器功能:
public function actionScheduleValidation()
{
print_r("in ajax");
$post_data = $_POST['myData'];
$decodedData = json_decode($post_data, true);
//$course = [[[]]];
$course=[];
$counter = 0;
//Save the years associated to sections chosen
foreach ($decodedData as $key) {
$tutOrLab = null;
$lec = null;
$currentYear = null;
foreach ($key as $id => $number) {
if ($id == 'year') {
$currentYear = $number;
} elseif ($id == 'subsection') {
$tutOrLab = Yii::app()->db->createCommand()
->select('courseID,kind,days,start_time,end_time,semester')
->from($id)
->where('id=' . $number)
->queryRow();
} else
$lec = Yii::app()->db->createCommand()
->select('courseID,kind,days,start_time,end_time,semester')
->from($id)
->where('id=' . $number)
->queryRow();
}
print_r(gettype($lec['start_time']));
$lecture = new Lecture($lec['courseID'],$lec['kind'],$lec['days'],$lec['start_time'],$lec['end_time'],$lec['semester'],$currentYear);
print_r(gettype($lecture->getStartTime()));
// WILL ACTUALLY DISPLAY SOMETHING
$tutorial = new TutorialAndLab($tutOrLab['courseID'],$tutOrLab['kind'],$tutOrLab['days'],$tutOrLab['start_time'],$tutOrLab['end_time'],$tutOrLab['semester'],$currentYear);
$course[$counter] = new CourseObj($lecture,$tutorial);
$counter++;
}
$courseYear1Fall = [];
$courseYear1Winter = [];
$courseYear2Fall = [];
$courseYear2Winter = [];
$courseYear3Fall = [];
$courseYear3Winter = [];
$courseYear4Fall = [];
$courseYear4Winter = [];
if($course != null) {
for ($i = 0; $i < count($course); $i++) {
if ($course[$i]->getLecture()->getYear() == '1') {
if ($course[$i]->getLecture()->getSemester() == 'F') {
array_push($courseYear1Fall, $course[$i]);
} elseif ($course[$i]->getLecture()->getSemester() == 'W') {
array_push($courseYear1Winter, $course[$i]);
}
} elseif ($course[$i]->getLecture()->getYear() == '2') {
if ($course[$i]->getLecture()->getSemester() == 'F') {
array_push($courseYear2Fall, $course[$i]);
} elseif ($course[$i]->getLecture()->getSemester() == 'W')
array_push($courseYear2Winter, $course[$i]);
} elseif ($course[$i]->getLecture()->getYear() == '3') {
if ($course[$i]->getLecture()->getSemester() == 'F') {
array_push($courseYear3Fall, $course[$i]);
} elseif ($course[$i]->getLecture()->getSemester() == 'W') {
array_push($courseYear3Winter, $course[$i]);
}
} elseif ($course[$i]->getLecture()->getYear() == '4') {
if ($course[$i]->getLecture()->getSemester() == 'F') {
array_push($courseYear4Fall, $course[$i]);
} elseif ($course[$i]->getLecture()->getSemester() == 'W') {
array_push($courseYear4Winter, $course[$i]);
}
}
}
$counter2=0;
$errorArr = [];
if($courseYear1Fall != null){
$fallErr = verification($courseYear1Fall);
$errorArr[$counter2] = $fallErr;
$counter2++;
}
elseif($courseYear1Winter != null) {
$winterErr = verification($courseYear1Winter);
$errorArr[$counter2] = $winterErr;
$counter2++;
}
if($courseYear2Fall != null) {
$fallErr = verification($courseYear2Fall);
$errorArr[$counter2] = $fallErr;
$counter2++;
}
if($courseYear2Winter != null) {
$winterErr = verification($courseYear3Fall);
$errorArr[$counter2] = $winterErr;
$counter2++;
}
if($courseYear3Winter != null) {
$fallErr = verification($courseYear3Fall);
$errorArr[$counter2] = $fallErr;
$counter2++;
}
if($courseYear3Fall != null) {
$winterErr = verification($courseYear3Winter);
$errorArr[$counter2] = $winterErr;
$counter2++;
}
if($courseYear4Fall != null) {
$fallErr = verification($courseYear4Fall);
$errorArr[$counter2] = $fallErr;
$counter2++;
}
if($courseYear4Winter != null) {
$winterErr = verification($courseYear4Winter);
$errorArr[$counter2] = $winterErr;
}
$this->renderPartial('_ajax', array(
'data' => $errorArr,
)
);
}
有关如何将其附加到原始(_gen.php)html代码的任何想法吗?
答案 0 :(得分:0)
您的ajax调用缺少分号。
作为一种好的做法,您不应该同时命名请求和响应变量“data”。