У меня есть класс с именем «Пример» и следующие методы
printProduct(),printCategory()
. У printProduct есть тег привязки, который вызывает функцию javascript через onclick и, используя ajax, я вызываю страницу php, где передаю значение текущего продукта.
private function _printProductLinks ($value_selected) {
$html = "";
$html .= "<ul class=\"tabs\">";
for($i=0; $i<count($this->_productArray); $i++) {
if($this->_productArray[$i]->_productId == $value_selected) {
$html .= "<li><a onclick=\"gotoProduct(".$this->_productArray[$i]->_productId.")\" id=\"selected\">" .$this->_productArray[$i]->_productName."</a></li>";
}else {
$html .= "<li><a onclick=\"gotoProduct(".$this->_productArray[$i]->_productId.")\">" .$this->_productArray[$i]->_productName."</a></li>";
}
}
$html .= "</ul>";
return $html;
}
private function _printCategoryLinks () {
$html = "";
$html .= "<ul class=\"tabs\">";
for($i=0; $i<count($this->_categoryArray); $i++) {
if($this->_categoryArray[$i]->_categoryId == $this->_currentCategory) {
$html .= $this->return_nbsp(2)."<li><a onclick=\"gotoCategory(".$this->_currentProduct.",".$this->_categoryArray[$i]->_categoryId.")\" id=\"selected\">" .$this->_categoryArray[$i]->_categoryName."</a></li>";
}else {
$html .= $this->return_nbsp(2)."<li><a onclick=\"gotoCategory(".$this->_currentProduct.",".$this->_categoryArray[$i]->_categoryId.")\">" .$this->_categoryArray[$i]->_categoryName."</a></li>";
}
}
$html .= "</ul>";
$html .= $this->_printSubCategoryLinks();
return $html;
}
и JavaScript gotoProduct () выглядит следующим образом
function gotoProduct(product_id){
try{
var xmlHttp = createXmlHttpRequestObject();
var jsonstring = "product=" +encodeURIComponent(product_id);
xmlHttp.open("POST", "dp_entry.php", true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", jsonstring.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
// var jsondata = xmlHttp.responseText;
}
}
xmlHttp.send(jsonstring);
} catch (e){
alert("Can't connect to server:\n" + e.toString());
}
}
так, при выполнении printProductlinks () вызовет функцию javascript gotoproduct (product_id) и затем напечатает категории с помощью _printcategories () в разделе products