Javascript没有';t在codeigniter中加载PHP函数,但在纯PHP环境中加载


Javascript doesn't load PHP function in codeigniter, but does in pure PHP environment

我有一个file.html,它试图在单击图像时调用neg.php(有许多<p class="bagi">.

<p class ="bagi">           
    <a href="try.html" onclick="return false;">                        
       <img src="images/neg.png" title ="Rate this negative" onclick="negative(this);">
       Try try try try try
    </a>
</p>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
    function negative(obj) {
       var url = obj.parentNode.valueOf('href');
       var name = obj.parentNode.innerText;
       alert(url);
       $("#quote p").load("neg.php?url="+ url + "&name=" + name);
    }
</script>
<div id="quote"><p></p></div>

neg.php与上面的代码位于同一个文件夹中,并将一个文本文件写入子文件夹cat/nonstatistics,如下所示:

<?php
header("Cache-Control: no-cache");
$url = $_GET["url"];
$name = $_GET["name"];
$file = 'cat/nonstatistics/' . $name . '.txt';
if (!file_exists($file)) {
    include_once 'classifier/classifier.php';
    include_once 'classifier/trainer.php';
    $current = file_get_contents($url);
    $current2 = strip_tags($current);
    $tr = new trainer();//I use PHP text classifier in this
    $arr = array('statistika', 'nonstatistika');
    $tr->makeCategory($arr);
    $cl = new classifier();
    $text = $current2;
    $result = $cl->classifyText($text);
    if ($result == 'nonstatistika') {
        file_put_contents($file, $current2);
        echo "Rated negative, Thanks for your response!";
    } else {
        echo "Rating failed";
    }
} else {
    echo "Nice..";
}
?>

它们运行得非常好,正是我想要的。但当我将这些代码转移到Codeigniter框架中时,我在其中放置了一个具有视图file.html的控制器,我发现它不起作用,函数在alert(url)处停止。我已经像<script type="text/javascript" src="<?php echo base_url() ?>js/jquery.js"></script>一样调用了jquery file

我也这样做了:

$("#quote p").load(base_url()+"views/load_hasil_cari/neg.php?url=" + url + "&name=" + name);

这个:

$file = base_url .'views/load_hasil_cari/cat/nonstatistics/' . $name . '.txt';

它们都不起作用。我点击了图片,它所做的就是alert(url)

我没有做什么?谢谢

编辑:这是控制器文件:

<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Retrain extends CI_Controller {
public function Retrain() {
    parent::__construct();
}
public function index() {
    $this->load->model('home_model');
    $data = $this->home_model->general();
    $data['file']=base_url().'application/views/load_hasil_cari/neg.php';        
    $this->load->view('load_hasil_cari/retrain_view', $data);
}
}

在Views文件夹中添加neg.php并尝试更改行

$("#quote p").load("neg.php?url="+ url + "&name=" + name);

附加控制器

$data['file']=base_url().'application/views/search/cat/nonstatistics/neg.php';

在视图中,使用更改neg.php

<?php echo $file;?>

如果有效,请尝试:

  1. 将neg.php放在项目文件夹的根目录下
  2. neg.php中的此行更改为$name = $_GET["name"];
  3. 将此行$("#quote p").load(base_url()+"views/search/neg.php?url=" + url + "&name=" + name);更改为$("#quote p").load(base_url()+"/neg.php?url=" + url + "&name=" + name);

让我们换一种方式:
这是js脚本

function negative(obj) {
   var url = obj.parentNode.valueOf('href');
   var name = obj.parentNode.innerText;
   alert(url);
   $.ajax({
        url     : '<?=base_url()?>/Retrain/load_files',
        type    : 'POST',
        data    : {'url' : url, 'name':name},
        success : function(resp){
            $("#quote p").html(resp);
        },
        error   : function(resp){
            //JSON.stringify(resp);     //uncomment it to alert the error
        }
   });
   //$("#quote p").load("neg.php?url="+ url + "&name=" + name);
}

这是一个新的控制器功能:

function load_files(){       ##controller function which will read the ajax request
    if($this->input->post(null)){
        $this->load->model('home_model');
        echo $this->home_model->load_file();  
    }
}

将返回ajax响应的模型函数:

#model function in file home_model.php
function load_file(){
    header("Cache-Control: no-cache");
    $url = $_GET["url"];
    $name = $_GET["name"];
    $file = 'cat/nonstatistics/' . $name . '.txt';
    if (!file_exists($file)) {
        include_once 'classifier/classifier.php';
        include_once 'classifier/trainer.php';
        $current = file_get_contents($url);
        $current2 = strip_tags($current);
        $tr = new trainer();//I use PHP text classifier in this
        $arr = array('statistika', 'nonstatistika');
        $tr->makeCategory($arr);
        $cl = new classifier();
        $text = $current2;
        $result = $cl->classifyText($text);
        if ($result == 'nonstatistika') {
            file_put_contents($file, $current2);
            return "Rated negative, Thanks for your response!";
        } else {
            return "Rating failed";
        }
    } else {
        return "Nice..";
    }
}