简体   繁体   English

Codeigniter显示ajax请求

[英]Codeigniter displaying ajax request

I'm a Codeigniter noob. 我是Codeigniter新手。 I've been trying for quite some time now to solve this. 我已经尝试了很长一段时间来解决这个问题。 I have a textview in my view.php. 我的view.php中有一个textview。 On button press, I want to send the text to server (php file) , process it and display result on page. 按下按钮时,我想将文本发送到服务器(php文件),进行处理并在页面上显示结果。 My current code is: 我当前的代码是:

javascript in view: 视图中的javascript:

function verify(){
                var posttext = $('#post_text').text();
                $.ajax({
                    type: "post",
                    url: "http://localhost/naloga1/CodeIgniter/index.php/usercontroller/checkinput",
                    cache: false,               
                    data: {post_text : posttext },
                    success: function(json){                        
                    try{        
                        var obj = jQuery.parseJSON(json);
                        alert( obj['STATUS']);


                    }catch(e) {     
                        alert('Exception while request..');
                    }       
                    },
                    error: function(){                      
                        alert('Error while request..');
                    }
             });
        }

usercontroller UserController的

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class UserController extends CI_Controller {


    public function checkinput(){
        $status = array("STATUS"=>"false");
        $text =  $_POST['post_text'];
        if ($text != null){
            $status = array("STATUS"=>"true");  
        }
        echo json_encode ($status) ;
        $output = $text.strip_tags();
        echo "<p>".$output."</p>";
    }   
}

and my textview 和我的textview

<textarea rows="3" cols="25" id="post_text" >Some random text</textarea>
    <input type="submit"  id="post_bttn" value="Post" onclick="verify()">

Any help would be greatly appreciated. 任何帮助将不胜感激。

function verify(){
        var posttext = $('#post_text').text();
        $.ajax({
            type: "post",
            url: "http://localhost/naloga1/CodeIgniter/index.php/usercontroller/checkinput",
            cache: false,               
            data: {post_text : posttext },
            success: function(json){                        
            try{        
                var obj = jQuery.parseJSON(json);
                alert( obj['STATUS']);


            }catch(e) {     
                alert('Exception while request..');
            }       
            },
            error: function(){                      
                alert('Error while request..');
            }
     });
}

Right now you are not passing any parameter to your Ajax request. 现在,您没有将任何参数传递给您的Ajax请求。 Your Data would be 您的数据将是

 data: {post_text:$('#post_text').text()},

And in controller file Use strip_tags() like below 并在控制器文件中使用strip_tags()如下

 $output = strip_tags($text,'<br><br/>');

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

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