0

我有这个file.html试图在neg.php单击图像时调用 a (有很多<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);
}

}
4

2 回答 2

0

在 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;?>
于 2013-07-22T06:29:05.990 回答
0

试试看是否有效:

  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..";
    }
}
于 2013-07-22T06:25:59.010 回答