How to fill textbox with database information afte

2019-09-08 05:54发布

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

2条回答
做自己的国王
2楼-- · 2019-09-08 06:50

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.

查看更多
时光不老,我们不散
3楼-- · 2019-09-08 06:52

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();
}
查看更多
登录 后发表回答