Javascript代码未使用php codeigniter执行
我正在我的项目中使用一些使用过的代码 以下代码用于在视图中编写的愿望列表中添加产品Javascript代码未使用php codeigniter执行,javascript,php,jquery,ajax,codeigniter,Javascript,Php,Jquery,Ajax,Codeigniter,我正在我的项目中使用一些使用过的代码 以下代码用于在视图中编写的愿望列表中添加产品 <p class="p"><a<?php echo "onclick='addtowishlist($product->id)'"; ?>>Add To Wishlist</a></p> 这是我的控制器命名车 public function addtowishlist($pID) { $data=$this->P
<p class="p"><a<?php echo "onclick='addtowishlist($product->id)'"; ?>>Add To Wishlist</a></p>
这是我的控制器命名车
public function addtowishlist($pID)
{
$data=$this->Product_model->addtowishlist($pID);
echo $data;
}
这是我的产品命名模型
function addtowishlist($ProductID) { echo "Added succesfully";
}
这是我在javascript中使用的用于远程调用的ajax函数
enter code herefunction remoteCall(sUrl, sQueryStr, sCalledBy){
uri = sUrl;
callingFunc = sCalledBy;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
if (xmlHttp)
{
xmlHttp.onreadystatechange = stateHandler;
xmlHttp.open("POST", sUrl, true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send(sQueryStr);
} }
我只想显示在我的控制器中写入的消息,单击“添加到愿望列表”链接时显示该消息
<p class="p"><a onclick="addtowishlist(<?php echo $product->id; ?>)">Add To Wishlist</a></p>
我认为问题在于,如果要将URL添加到客户端代码,请使用
site\u URL()
函数。更改此部分:
function addtowishlist(pid) { remoteCall("cart/addtowishlist/"+pid,"","loadlist"); }
function loadlist() { alert(sResponse); }
致:
函数addtowishlist(pid){remoteCall(“,”,“loadlist”);}
函数loadlist(){alert(sResponse);}
当然,这部分代码应该在PHP文件中,以便能够处理PHP部分
为了记录在案,处理此类情况的最佳方法是使用JQuery库进行AJAX操作,并将相关数据添加到一些属性中,如data href
或其他属性。是否调用addtowishlist
?removeCall
是否正在向服务器发送请求?什么时候会失败?是的,即使我在addtowishlist中的alert中写入一些硬编码值,它也会被调用,然后它也会被调用“xmlHttp.onreadystatechange=stateHandler;”-您的stateHandler在哪里?函数stateHandler(){if(xmlHttp.readyState==4){sResponse=xmlHttp.responseText;eval(callingFunc+“()”)}返回真实;}你的方式和他的方式一样!!
function addtowishlist(pid) { remoteCall("cart/addtowishlist/"+pid,"","loadlist"); }
function loadlist() { alert(sResponse); }
function addtowishlist(pid) { remoteCall("<?php echo site_url('cart/addtowishlist/'+pid) ?>","","loadlist"); }
function loadlist() { alert(sResponse); }