我有一個主要的選擇列表的驅動器上的各種事情的課程。當選擇課程時,另一個選擇列表將被重新填入,並且該課程的開課日期可提前6個月。此外,我在學生的姓名和電話號碼頁面上有一個表格,當選擇一門課程時,表格將與所有註冊該課程的學生重新填寫。我的問題是我會通過JSON從PHP獲取各種不同的東西,即學生和開始日期。因此,我如何將多個事件傳遞給jQuery?如果課程選擇列表不僅影響2件事,而且影響3,5或甚至10件?我們如何處理PHP和jQuery?如何將PHP中的多個數據傳遞給jQuery/AJAX?
foreach($m as $meta)
{
$metaCourse = $this->getCourseInfo($meta['parent_course']);
//populate the select list with the name of each course
$metaSelectList .= '<option id="select'.$count.'" value="'.$metaCourse['id'].'">'.$metaCourse['fullname'].'</option>';
$count++;
//get only the first course's dates
if($count3 == 1)
{
$startDate = intval($course->getStartDate(50));
$endDate = strtotime('+6 month', $startDate);
//populates the select list with the starting date of the course up to the next six months
for($date = $startDate; $date <= $endDate ; $date = strtotime('+1 day', $date))
{
$dateSelectList .= '<option id="select'.$count2.'" value="'.$date.'">'.date('D d F Y', $date).'</option>';
$count2++;
}
$count3++;
$students = $s->getStudents($metaCourse['id']);
$content = $this->createStudentTable($students);
}
}
這是我對AJAX的處理程序...現在(我還沒有實現學生的表格中,因爲我仍然在試圖弄清楚如何將多個數據塊傳遞給jQuery的)。基本上每次選擇課程時,PHP都會創建一個包含適當日期的新選擇列表,然後將其傳遞給jQuery。我不確定是否應該在JavaScript或PHP中執行此操作。
if (isset($_GET['pid']) && (isset($_GET['ajax']) && $_GET['ajax'] == "true"))//this is for lesson select list
{
$pid = intval($_GET['pid']);
$c = new CourseCreator();
$startDate = intval($c->getStartDate($pid));
$endDate = strtotime('+6 month', $startDate);
$dateSelectList = '<select name="dateSelect" id="dateSelect">';
//populates the select list with the starting date of the course up to the next six months
for($date = $startDate; $date <= $endDate ; $date = strtotime('+1 day', $date))
{
$dateSelectList .= '<option id="select'.$count2.'" value="'.$date.'">'.date('D d F Y', $date).'</option>';
$count2++;
}
$dateSelectList .= '</select>';
echo json_encode($dateSelectList);
exit;
}
我的jQuery的處理程序:
$('#metaSelect').live('change', function()
{
$.getJSON('?ajax=true&pid='+$('#metaSelect').val(), function(data)
{
alert(data);
$('#dateSelectDiv').html(data);
});
});
謝謝:)是啊,這是我的計劃是到所有的PHP的東西搬到jQuery的,所以我不會在後臺進行生成HTML。我只需要找到一種方法將大數據傳遞給jQuery – 2012-03-26 20:56:45
我剛剛實現了您的代碼片段,它們的工作非常棒。非常感謝:) – 2012-03-27 09:56:52
甜蜜,很高興聽到:) – DdD 2012-03-28 02:09:15