我使用$ .each通过ajax从php获取数据但是当我使用它时我的浏览器说这个错误未捕获TypeError:无法读取属性'length'的null,无法找到任何答案。
$('#search-button-applicant').click(function (){
date_to = Date.parse($('#search-to-date').val());
date_from = Date.parse($('#search-from-date').val());
applicant_name = $('#search-applicant-name').val();
if($('#search-to-date').val() == "" && $('#search-from-date').val() == "" && applicant_name == "" )
{
alert("you can not search");
}
else if($('#search-to-date').val() != "" && $('#search-from-date').val() == "" || $('#search-to-date').val() == "" && $('#search-from-date').val() != ""){
alert("you can not search");
}
else{
var date_to_val = $('#search-to-date').val();
var date_from_val = $('#search-to-date').val();
var loan_type_val = $('#loan-type').val();
var search_word_val = $('#search-applicant-name').val();
$.ajax({
type: "post",
url: "filter_search_window_get.php",
dataType:'json',
data: {'func_num':loan_type_val,'Date_To':date_to_val,'Date_From':date_from_val,'search_word':search_word_val},
success: function(data){
$.each(data, function(i, item) {
$('#search-table-content-list').append("<tr><td style='width:7.5%;'><label>Date Filled</label></td><td style='width:9.5%;'><label>Date Released</label></td><td style='width:8%;'><label>"+data[i].last_name+"</label></td><td style='width:25%;'><label>Name</label></td><td style='width:7.5%;'><label>Term</label></td><td style='width:7.5%;'><label>Rate</label></td><td style='width:7.5%;'><label>Principal</label></td><td style='width:7.5%;'><label>Interest</label></td><td style='width:7.5%;'><label>Total</label></td></tr>");
});
}
});
}
});
php:
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
$loan_type =$_POST['loan_type_val'];
switch ($loan_type){
case '1':
tuition_get();
break;
case '2':
salary_get();
break;
case '3':
sss_get();
break;
case '4':
ofw_get();
break;
case '5':
business_get();
break;
}
function tuition_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
$query = "SELECT
a.applicant_id,
a.last_name,
a.first_name,
a.created_date,
b.form_type,
b.form_id,
c.stat,
d.release_date,
f.payment_term,
j.form_rate,
k.loan_amt,
k.interest_amt,
k.total_amt
FROM
applicant a
LEFT JOIN form_type b ON (a.form_id = b.form_id)
LEFT JOIN form_status c ON (c.status_id = a.status_id)
LEFT JOIN applicant_loan_repayment d ON (d.applicant_id = a.applicant_id)
LEFT JOIN applicant_tuition_loan_dtl f ON (f.applicant_id = a.applicant_id)
LEFT JOIN form_term j ON (j.form_id = a.form_id)
LEFT JOIN applicant_loan_repayment k ON (k.applicant_id = a.applicant_id)
WHERE a.last_name LIKE '%$search_word%' OR a.first_name LIKE '%$search_word%' ";
$result=mysql_query($$query)or die(mysql_error());
$data=array();
while ($row = mysql_fetch_array($result))
{
$data[] = array(
'last_name'=>$row['last_name'],
'first_name'=>$row['first_name'],
'form_typ'=>$row['form_type']
);
}
echo json_encode($data);
}
function salary_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
}
function sss_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
}
function ofw_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
}
function business_get(){
$date_from = $_POST['date_from_val'];
$date_to = $_POST['date_to_val'];
$search_word = $_POST['search_word_val'];
}
我使用$ .each从查询中获取所有获取数据并使用ajax将其发布到表中,但我的$ .each无法正常工作,我不知道我的代码是错误的。
答案 0 :(得分:1)
看起来您正在通过ajax发布名为func_num
的参数,但尝试在php中将其作为loan_type_val
读取。
此外,所有您传递的数据的名称与您尝试在服务器端读取的数据不同。
如果您尝试通过帖子发送:
'func_num':loan_type_val,
'Date_To':date_to_val,
'Date_From':date_from_val,
'search_word':search_word_val
您的PHP服务器端必须是:
$loan_type = $_POST['func_num'];
$date_to = $_POST['Date_To'];
$date_from = $_POST['Date_From'];
$search_word = $_POST['search_word'];