How to fill textbox with database information after another field lostfocus

余生长醉 提交于 2020-01-06 08:10:10

问题


Could you help me find a way to solve my problem please?

I have 4 textbox fields : number , key, firstname and name.

When I tabulate from the number field after filling it, I would like to check in my database if this number exists.

If so, my application should select the name and firstname linked to the number (in my database) and fill in the other fields name and firstname and then disable these fields.

I think that I should use ajax but i don't know how to use ajax and Zend to achieve it.

I searched the web and I found a few tutorial that I don't understand.

Please could you give me a step by step way to do it?

Thanks


回答1:


Just general brushstrokes:

  1. Generate your form in the standard way using Zend_Form.
  2. On the client-side (using jQuery, for example), attach an onchange handler to the number field.
  3. The onchange handler makes an AJAX call to an action called something like checkNumberExistsAction() that performs your db lookup. Return JSON formatted info containing the results of your check.
  4. The success function of your AJAX call then checks the return result, populates and disables the other elements.

One thing to remember: Do not depend solely upon this client-side processing to prevent the submission of disabled fields. Users can disable scripts on the client-side, so be sure to have server-side validation in place, as well.




回答2:


i fully agree to @David approach and to commemorate the same, posts this skeleton code:

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();
}


来源:https://stackoverflow.com/questions/8811954/how-to-fill-textbox-with-database-information-after-another-field-lostfocus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!