使用ajax从javascript函数调用php函数

I have a class named "Example" and has following methods printProduct(),printCategory(). The printProduct has an anchor tag which has call to javascript function through onclick and using ajax i call a php page where i pass the value of current product.

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;
    }

and the javascript gotoProduct() is as follows

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:
" + e.toString());
    }
}

so,on executing the printProductlinks() will call javascript function gotoproduct(product_id) and then has to print the categories using _printcategories() under the products

dtzjvj3915
dtzjvj3915 那就是应该从javascript函数调用php函数gotoproduct()
9 年多之前 回复
dougan7523
dougan7523 printCategoryLinks()在哪里被调用?在dp_entry.php里面?如果是这样,那么我们也需要看到它
9 年多之前 回复
douzhen5158
douzhen5158 该死的,提供一些PHP和JS代码,我们无法知道你在做什么......
9 年多之前 回复
douting0585
douting0585 执行gotoProduct()后,应该执行printCategoryLinks()。
9 年多之前 回复
doutidi5037
doutidi5037 我糊涂了。究竟是什么问题?
9 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问