2011-05-12 75 views
1

我試圖開發使用ajax的職位和列表選擇更新後者從MySQL數據庫的響應的應用程序,但列表顯示爲空,可以將部分一個扶從這個我出去,請.....爲的.js安裝webOS的列表選擇的選擇與AJAX JSON響應動態

代碼:爲PHP

SecondAssistant.prototype.setup = function() { 
this.selectorChanged = this.selectorChanged.bindEventListener(this); 
Mojo.Event.listen(this.controller.get('firstselector'), Mojo.Event.propertyChange,  this.selectorChanged); 

this.names = []; 

try { 
    new Ajax.Request('http://localhost/projects/testingasdf.php', { 
     method: 'post', 
     parameters: { 
     'recs': getallrecords, 
     'q': q 
     }, 
     evalJSON: 'true', 
     onSuccess: function(response){ 
      var json = response.responseJSON; 
      var count = json.count - 1; 

      for(i=0; i<count; i++){ 
       this.names.push({ 
        label: json[i].name, 
        value: '0' 
       }); 
      } 
         this.controller.modelChanged(this.model); 
     }.bind(this), 
     onFailure: function(){ 
      Mojo.Controller.errorDialog('Failed to get ajax response'); 

     } 

    }); 

} 
catch (e){ 
    Mojo.Controller.errorDialog(e); 
} 

this.controller.setupWidget("firstselector", 
      this.attributes = { 
       label: $L('Name'), 
       modelProperty: 'currentName' 
      }, 
      this.model = { 
       choices: this.names 
      } 
     ); 

}; 

代碼:

<?php 
header('Content-type: application/json'); // this is the magic that sets responseJSON 


$conn = mysql_connect('localhost', 'root', '')// creating a connection 



mysql_select_db("test", $conn) or die('could not select the database');//selecting database from connected database connection 

switch($_POST['recs']) 
{ 
    case'getallRecords':{ 
     $q = $_POST['q']; 

     //performing sql operations 
     $query = sprintf("SELECT * FROM user WHERE name= $q"); 
     $result = mysql_query($query) or die('Query failed:' .mysql_error()); 
     $all_recs = array(); 
     while ($line = mysql_fetch_array($result,MYSQL_ASSOC)) { 
     $all_recs[] = $line; 
     } 
     break; 
     } 
     } 

echo json_encode($all_recs); 

// Free resultset 
mysql_free_result($result); 


// closing connection 
mysql_close($conn); 
?> 
+0

我試圖相同,但B =使用遠程託管服務器..本地主機是不是爲我工作:( – 2011-06-21 12:48:29

回答

1

我會移動的模型更新代碼出來的SecondAssistant.prototype.setup方法,並讓它在SecondAssistant.prototoype.activate的某個地方啓動。

也叫modelChanged

this.controller.modelChanged(this.model); 

有上bindEventListener一個錯字 - 應該是bindAsEventListener和綁定的迴歸應該是一個不同的對象:

this.selectorChangedBind = this.selectorChanged.bindAsEventListener(this); 
+0

感謝您的回覆,我已經添加了this.controller.modelChanged(this.model); – raj 2011-05-13 08:47:03

+0

我已經修改了代碼,並更新,但日誌消息顯示一些錯誤 錯誤:錯誤:無法調用未定義的方法「bindAsEventListener」,行定義,文件未定義 警告:WidgetController:無法實例化插件「firstselector」,因爲它尚未建立。 任何建議,請.... :( – raj 2011-05-13 09:14:22

+0

@raj - 上bindEventListener評審編輯答案應該是bindAsEventListener和對象應該是一個不同的變量。 – 2011-05-13 14:44:37