簡體   English   中英

Codeigniter顯示ajax請求

[英]Codeigniter displaying ajax request

我是Codeigniter新手。 我已經嘗試了很長一段時間來解決這個問題。 我的view.php中有一個textview。 按下按鈕時,我想將文本發送到服務器(php文件),進行處理並在頁面上顯示結果。 我當前的代碼是:

視圖中的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的

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM