简体   繁体   English

JavaScript代码未使用php codeigniter执行

[英]Javascript code is not executing with php codeigniter

I am using some used code in my project 我在项目中使用了一些二手代码

The following code is used to add the product in the wishlist written in view 以下代码用于在视图中编写的愿望清单中添加产品

<p class="p"><a<?php  echo "onclick='addtowishlist($product->id)'";  ?>>Add To Wishlist</a></p>

The Javascript code for this 为此的Javascript代码

  function addtowishlist(pid) {   remoteCall("cart/addtowishlist/"+pid,"","loadlist"); }
 function loadlist() { alert(sResponse); }

This is my controller naming cart 这是我的控制器命名购物车

   public function addtowishlist($pID)
    {

    $data=$this->Product_model->addtowishlist($pID);
    echo $data;

    }

This is my Model naming Product_model 这是我的模型命名Product_model

function addtowishlist($ProductID) { echo "Added succesfully";
}

This is my ajax for remote call function used in javascript 这是我在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);
}   }

I only want to show the message written in my controller to displayed when clicked on the Add to wishlist link 我只想显示在控制器中编写的消息,然后单击“添加到愿望清单”链接时显示

这就是您可以调用该函数的方式

<p class="p"><a onclick="addtowishlist(<?php echo $product->id; ?>)">Add To Wishlist</a></p>

I think the problem is because if the URL, for adding URL to your client side code use site_url() function. 我认为问题是因为,如果将URL添加到客户端代码中,则使用site_url()函数。 change this part: 更改此部分:

 function addtowishlist(pid) {   remoteCall("cart/addtowishlist/"+pid,"","loadlist"); }
 function loadlist() { alert(sResponse); }

to: 至:

function addtowishlist(pid) {   remoteCall("<?php echo site_url('cart/addtowishlist/'+pid) ?>","","loadlist"); }
 function loadlist() { alert(sResponse); }

Of course this part of code should be in PHP file to be able to handle the PHP part. 当然,这部分代码应该在PHP文件中,以便能够处理PHP部分。


Just for the record the best way for handling these kinds of situation is using JQuery library for AJAX actions and adding the related data to some attribute like data-href or other ones. 仅作记录,处理此类情况的最佳方法是将JQuery库用于AJAX操作,并将相关数据添加到某些属性(如data-href或其他属性)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM