2012-01-10 49 views
0

你能幫我找到一種方法來解決我的問題嗎?如何在數據庫信息填充文本框後另一個字段lostfocus

我有4個文本框字段:number,key,firstnamename

當我填寫後從數字字段列表中,我想檢查我的數據庫,如果這個數字存在。

如果是這樣,我的應用程序應該選擇鏈接到數字(在我的數據庫中)的名稱和名字,並填寫其他字段名稱和名字,然後禁用這些字段。

我認爲我應該使用ajax,但我不知道如何使用ajax和Zend來實現它。

我搜索了網頁,發現一些我不明白的教程。

請你能給我一步一步的方式來做到這一點嗎?

感謝

+0

您是否想在顯示頁面時填充表單?請用您當前的代碼描述您的問題。我們可以幫助你。 – 2012-01-11 06:12:34

回答

1

只是一般的筆觸:

  1. 生成使用Zend_Form的標準方式的形式。
  2. 在客戶端(例如使用jQuery),將onchange處理程序附加到數字字段。
  3. onchange處理程序將一個名爲類似checkNumberExistsAction()的動作的AJAX調用執行您的數據庫查找。返回包含您的支票結果的JSON格式信息。
  4. 您的AJAX調用的成功函數然後檢查返回結果,填充和禁用其他元素。

有一點要記住:不要僅僅依靠這個客戶端處理來防止提交被禁用的字段。用戶可以在客戶端禁用腳本,因此請確保具有服務器端驗證。

0

我完全同意@大衛的做法,並紀念相同,職位代碼框架:

sampleview.phtml

echo '<div id="a">'.$this->form.'</div>'; 

<?php $this->jQuery()->onLoadCaptureStart(); ?> 
jQuery('#category').change(checkNumberExistsAction); 
<?php $this->jQuery()->onLoadCaptureEnd(); ?> 


<script type="text/javascript"> 
    function checkNumberExistsAction(){   
    var p = $("#idOfNumberField").val(); 
    var response = $.ajax({ 
     url: "<?php echo $this->url(array('controller'=>'index', 
'action'=>'sampleview')) ?>", 
     type: "GET",  
     data: {number: p}, //where number is number columnName in database 
     cache: false, 
     success: function(text){ 
      response = text; 
      $("#name").val($(text).find("input[name='name']").val()); //where name is id of name field 
      $("#name").attr("disabled", "disabled"); 
      $("#firstName").val($(text).find("input[name='firstName']").val()); //where firstName is id of first name field 
      $("#firstName").attr("disabled", "disabled");    
}, 
error: function(){ alert('Something went wrong.'); } 
    }); 
} 
</script> 

IndexController.php

public function sampleviewAction(){ 
    if ($this->getRequest()->isXmlHttpRequest()){ 
     $id = $this->_getParam('number', 0); 
     if ($id > 0){ 
     $albums = new Application_Model_DbTable_Albums(); 
     $form->populate($albums->getAlbum($id));}  
     } 
} 

Modelsampleview.php

public function getAlbum($id){ 
    $id = (int)$id; 
    $row = $this->fetchRow('e_recNo = ' . $id); 
    if (!$row){ 
    throw new Exception("Could not find row $id"); 
    } 
    return $row->toArray(); 
} 
相關問題