显示ajax请求的编码器

Codeigniter displaying ajax request

本文关键字:编码器 请求 ajax 显示      更新时间:2023-09-26

我是一个Codeigniter新手。我花了很长时间来解决这个问题。我在view。php中有一个textview。按下按钮,我想把文本发送到服务器(php文件),处理它,并在页面上显示结果。我现在的代码是:

javascript in view:

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..');
                    }
             });
        }

用户控件

<?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>";
    }   
}

和我的textview

<textarea rows="3" cols="25" id="post_text" >Some random text</textarea>
    <input type="submit"  id="post_bttn" value="Post" onclick="verify()">
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..');
            }
     });
}

现在您没有向Ajax请求传递任何参数。您的数据将是

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

在控制器文件中使用下面的strip_tags()

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