这一定是一个简单的语法错误,但我无法理解。所以在我的 HTML 中我有一些空标签 (tbody)
<label for="matiere2">Classe :</label>
<select name="matiere2" id="matiere1" onchange="showList(this.value)">
<option></option>
'.$this->addClasseToselect().'
</select>
<div class="tableau">
<table>
<caption>Mes listes</caption>
<thead>
<tr>
<th>Quantité</th>
<th>Libellé</th>
<th>Supprimer</th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
我也有 select with onchange ajax 函数来获取表中的正确数据 这是我的 js 文件,只有一个 ajax 函数
function showList(str) {
if (str == "") {
document.getElementById("tbody").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
var versions = ["MSXML2.XmlHttp.5.0",
"MSXML2.XmlHttp.4.0",
"MSXML2.XmlHttp.3.0",
"MSXML2.XmlHttp.2.0",
"Microsoft.XmlHttp"]
for(var i = 0, len = versions.length; i < len; i++) {
try {
xmlhttp = new ActiveXObject(versions[i]);
break;
}
catch(e){}
} // end for
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("tbody").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","vue/getliste.php?q="+str,true);
xmlhttp.send();
}
} 函数中调用的php文件(getliste.php):
<?php
include_once ('../modele/liste.php');
$q = intval($_GET['q']);
$res='';
foreach($listes=liste::findAllByIdAndClasse($q) as $c){
var_dump($c);
$res.='<tr><td>'.$c->__get['quantite'].'</td><td>'.$c->__get['intitule'].'</td><td><a class="del_button" type="button" onclick="document.getElementById(\'light\').style.display=\'block\';document.getElementById(\'fade\').style.display=\'block\'" id="">X</a></td></tr>';
}
echo $res;
这是我从 var_dump() 得到的结果,所以我的数据已正确加载:
object(liste)[3]
private 'id' => string '23' (length=2)
private 'quantite' => string '7' (length=1)
private 'intitule' => string 'Mcdo' (length=4)
private 'idClasse' => string '18' (length=2)
private 'idMatiere' => string '11' (length=2)
private 'idProf' => string '53' (length=2).
这是我的类“liste”及其属性,getliste.php 中使用的 __get 方法和 findAllByIdAndClasse(id) 方法 类列表 {
private $id;
private $quantite;
private $intitule;
private $idClasse;
private $idMatiere;
private $idProf;
function __construct()
{
}
public function __get($attr_name) {
if (property_exists( __CLASS__, $attr_name)) {
return $this->$attr_name;
}
$emess=__CLASS__ . ": unknown member $attr_name (getAttr)";
throw new Exception($emess, 45);
}
和 findAllByIdAndName(id) ,仍然在 liste.php 中
public static function findAllByIdAndClasse($q){
session_start();
$c=base::getConnection();
$query=$c->prepare("select * from liste WHERE idProf=:idProf AND idClasse=:idClasse ORDER BY idClasse ASC ");
$query->bindParam(':idProf',$_SESSION['userid'],PDO::PARAM_INT);
$query->bindParam(':idClasse',$q,PDO::PARAM_INT);
$dbres=$query->execute();
$d=$query->fetchAll();
$tab=array();
foreach ($d as $key => $value) {
$a=new liste();
$a->id=$value['id'];
$a->quantite=$value['quantite'];
$a->intitule=$value['intitule'];
$a->idClasse=$value['idClasse'];
$a->idProf=$value['idProf'];
$a->idMatiere=$value['idMatiere'];
$tab[]=$a;
}
return $tab;
}
我收到一个 php 错误,指出我正在尝试获取一个不存在的属性,我检查了它的拼写,它似乎是正确的,我唯一的线索是它是一个语法错误,有一些 ' "问题但是我无法理解...感谢任何帮助,谢谢!
编辑
所以现在我尝试在 __get 函数中的 if 之前添加一个 var_dump,所以它变成了:
public function __get($attr_name) {
var_dump($attr_name);
if (property_exists( __CLASS__, $attr_name)) {
return $this->$attr_name;
}
$emess=__CLASS__ . ": unknown member $attr_name (getAttr)";
throw new Exception($emess, 45);
} 我从中得到的结果是:
string '__get' (length=5)
这意味着这一行:
$res.='<tr><td>'.$c->__get['quantite'].'</td><td>'.$c->__get['intitule'].'</td></tr>';
正在发送 $c->__get[__get]....嗯,我不明白...
最佳答案
你的最后一行代码有错误:
$res.='<tr><td>'.$c->__get['quantite'].'</td><td>'.$c->__get['intitule'].'</td></tr>';
应该是……
$res.='<tr><td>'.$c->quantite.'</td><td>'.$c->intitule.'</td></tr>';
“__get”函数使用“魔法”捕获未定义的属性。在您的代码中,您尝试读取的未定义属性是“__get”,这是您 var_dump 显示的内容。
查看 PHP 手册了解更多详细信息... http://php.net/manual/en/language.oop5.magic.php
关于javascript - Ajax 调用正常但 php 文件在从对象检索属性时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560462/