doudao3170 2011-03-07 06:40
浏览 82

使用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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛