1

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.

3
  • what issue you are facing with this code? Commented Aug 21, 2015 at 6:31
  • echo json_encode ($status) ; $output = $text.strip_tags(); echo "<p>".$output."</p>"; why are you echo two times Commented Aug 21, 2015 at 6:37
  • I didn't know there was anything wrong with that? is it? Commented Aug 21, 2015 at 6:40

2 Answers 2

2
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..');
            }
     });
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried it and it throws me the Exception while request error, am I missing something?
I dont't get any error apart from this javascript alert
$output = $text.strip_tags(); echo "<p>".$output."</p>"; this thing is not encode in json, so you need to encode it and try to keep all in one echo only encode everything and only echo for one time
0

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/>');

9 Comments

Thank you, apparently this was my first mistake, but it still throws me an error..
alert('Exception while request..'); This part of js appears (a window with this text)
Thank you, but it still throws me the same exception. This is weird.
Now problem is with try var obj = jQuery.parseJSON(json); alert( obj['STATUS']); just alert(json); on it and check
try{ alert(json); } and check the result
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.