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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。