简体   繁体   中英

Codeigniter displaying ajax request

I'm a Codeigniter noob. I've been trying for quite some time now to solve this. I have a textview in my view.php. On button press, I want to send the text to server (php file) , process it and display result on page. My current code is:

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

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

<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. Your Data would be

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

And in controller file Use strip_tags() like below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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