2013-02-02 72 views
1

我有這樣的代碼在我的視圖文件(searchV.php):笨:從視圖中傳遞數據給控制器不工作

<html> 
    <head> 
     <title>Search Domains</title> 
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
     <script> 
      function noTextVal(){ 
       $("#domaintxt").val(""); 
      } 

      function searchDom(){ 
       var searchTxt = $("#searchTxt").val(); 
       var sUrl = $("#url").val(); 

       $.ajax({ 
        url : sUrl + "/searchC", 
        type : "POST", 
        dataType : "json", 
        data : { action : "searchDomain", searchTxt : searchTxt }, 
        success : function(dataresponse){ 
         if(dataresponse == "found"){ 
          alert("found"); 
         } 
         else{ 
          alert("none"); 
         } 
        } 

       }); 
      } 
     </script> 
    </head> 
    <body> 

     <form id="searchForm"> 
       <input type="text" id="searchTxt" name="searchTxt" onclick="noTextVal()" > 
       <input type="submit" id="searchBtn" name="searchBtn" value="Search" onclick="searchDom()" /> 
       <input type="hidden" id="url" name="url" value="<?php echo site_url(); ?>" /> 
     </form> 

     <?php 
      var_dump($domains); 
      if($domains!= NULL){ 
       foreach ($domains->result_array() as $row){ 
        echo $row['domain'] . " " . $row['phrase1']; 
        echo "<br/>"; 
       } 
      } 

     ?> 

    </body> 
</html> 

以下是我的控制器(searchC.php):

<?php 

class SearchC extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('searchM'); 
    } 

    public function index() 
    { 
     $data['domains'] = $this->searchM->getDomains(); 
     $this->load->view('pages/searchV', $data); 



     switch(@$_POST['action']){ 
      case "searchDomain": 
       echo "test"; 
       $this->searchDomains($_POST['searchTxt']); 
       break; 
      default: 
       echo "test2"; 
       echo "<br/>action:" . ($_POST['action']); 
       echo "<br/>text:" . $_POST['searchTxt']; 
     } 
    } 

    public function searchDomains($searchInput) 
    { 
     $data['domains'] = $this->searchM->getDomains($searchInput); 
     $res = ""; 

     if($data['domains']!=NULL){ $res = "found"; } 
     else{ $res = "none"; } 

     echo json_encode($res); 
    } 
} //end of class SearchC 

?> 

現在我已經做了一個測試控制器上使用開關來檢查傳遞的JSON數據是否成功,但它總是顯示未定義..這裏有什麼問題?有人可以解釋爲什麼數據在控制器中無法識別嗎?

+0

不應該通過'$ this-> input-> post('searchTxt');' – ekims

+0

使用firebug來查看你從控制器得到的響應... –

回答

1

您未通過網址傳遞數據,因此您需要使用$this->input->post()來檢索數據。

例如,

public function searchDomains() 
{ 
    $data['domains'] = $this->searchM->getDomains($this->input->post('searchTxt')); 
    $res = ""; 

    if($data['domains']!=NULL){ $res = "found"; } 
    else{ $res = "none"; } 

    echo $res; 
} 
+0

試過這個&我得到​​的錯誤不再存在..但我的看法警報不工作..沒有打印..所以它似乎回聲json_encode($ res);不工作.. – Leah

+0

刪除json_encode如果你只是返回一個字符串。如果你計劃返回一個對象或數組,然後使用'json_encode'。我已更新我的答案 – ekims

+0

@ekims也可以使用$ _POST,但推薦使用$ this-> input-> post。 –

0

我相信,數據被正確返回,但問題是你的代碼檢查。 $ .ajax函數解析JSON並將其轉換爲JavaScript對象。因此,您需要修改您的代碼,如下所示:

if(dataresponse.res == "found"){ // Changed from dataresponse to dataresponse.res 
    alert("found"); 
} 
else{ 
    alert("none"); 
} 

這應該適合您。

相關問題