2013-07-11 10 views
12

私はPOSTメソッドを使って多くのチュートリアルを検索しましたが、ここでも答えられた質問が表示されましたが、私のPOSTはまだ機能しません...あなたが私が何かを見たら、 't!JQuery Ajax POST in Codeigniter

私のJS - messages.js:

$(document).ready(function(){ 

    $("#send").click(function() 
    {  
    $.ajax({ 
     type: "POST", 
     url: base_url + "chat/post_action", 
     data: {textbox: $("#textbox").val()}, 
     dataType: "text", 
     cache:false, 
     success: 
       function(data){ 
       alert(data); //as a debugging message. 
       } 

    return false; 
}); 
}); 

マイビュー - chat.php:

<?php $this->load->js(base_url().'themes/chat/js/messages.js');?> //i use mainframe framework which loading script this way is valid 



<form method="post"> 
    <input id="textbox" type="text" name="textbox"> 
    <input id="send" type="submit" name="send" value="Send"> 
</form> 

最終私のコントローラ - chat.php

//more functions here 

function post_action() 
{ 
    if($_POST['textbox'] == "") 
    { 
     $message = "You can't send empty text"; 
    } 
    else 
    { 
     $message = $_POST['textbox']; 
    } 
    echo $message; 
} 
+0

エラーが発生しました。あなたが開発者用ツールコンソールを確認してください。 – bipen

+0

コントローラに行くのですか?どのようなエラーがスローされますか? –

+0

ここで 'url:base_url'を定義していますが、これはあなたの問題だと思います。 – Boris

答えて

20
$(document).ready(function(){ 

    $("#send").click(function() 
    {  
    $.ajax({ 
     type: "POST", 
     url: base_url + "chat/post_action", 
     data: {textbox: $("#textbox").val()}, 
     dataType: "text", 
     cache:false, 
     success: 
       function(data){ 
       alert(data); //as a debugging message. 
       } 
      });// you have missed this bracket 
    return false; 
}); 
}); 
+0

あなたはブラケットを見逃しています。 JavaScriptがちょうどリフレッシュ –

+1

はい、ありがとうございました。それは私の問題でした...私は何時間もそれを探しました! – Dennis

+0

コントローラはjsonでエンコードされたオブジェクトを返します。どのようにしてこのオブジェクトをPHPスクリプトに入れることができますか?つまり、jsonオブジェクトをajaxの成功関数からPHPスクリプトに渡すことができます。 – 151291

2

質問が既に回答されていますが、私はまた、ので、あなたのコントローラのコードは次のようになり、あなたが知っているものではなく、私はあなたがCodeIgniter input classを使用reccomendネイティブのPHPの$ _POSTを使用するだろうと思った

function post_action() 
{ 
    if($this->input->post('textbox') == "") 
    { 
     $message = "You can't send empty text"; 
    } 
    else 
    { 
     $message = $this->input->post('textbox'); 
    } 
    echo $message; 
} 
+0

codeigniter入力クラスを使用する理由を説明した場合、つまり$ _POSTよりも利点は何ですか? – haakym

+0

CIの入力クラスを使用する利点は、自動xss_filteringが含まれていることです(configで無効にされていない限り) – Dennis

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

    class UserController extends CI_Controller { 

     public function verifyUser() { 
      $userName = $_POST['userName']; 
      $userPassword = $_POST['userPassword']; 
      $status = array("STATUS"=>"false"); 
      if($userName=='admin' && $userPassword=='admin'){ 
       $status = array("STATUS"=>"true"); 
      } 
      echo json_encode ($status) ;  
     } 
    } 


function makeAjaxCall(){ 
    $.ajax({ 
     type: "post", 
     url: "http://localhost/CodeIgnitorTutorial/index.php/usercontroller/verifyUser", 
     cache: false,    
     data: $('#userForm').serialize(), 
     success: function(json){       
     try{   
      var obj = jQuery.parseJSON(json); 
      alert(obj['STATUS']); 


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

codeigniterでは、ajax postメソッドで "data"を調べる必要はありません。ここには があります。

searchThis = 'This text will be search'; 
    $.ajax({ 
     type: "POST", 
     url: "<?php echo site_url();?>/software/search/"+searchThis, 
     dataType: "html", 
     success:function(data){ 
     alert(data); 
     }, 

    }); 

注:URLに、ソフトウェアはコントローラ名で、検索は、関数名であるとsearchThisは私が送るmは変数です。

ここにコントローラがあります。

public function search(){ 
    $search = $this->uri->segment(3); 
     echo '<p>'.$search.'</p>'; 
    } 

私はあなたの仕事のアイデアを得ることができたら幸いです。