任务:当我从选择标签客户(我有客户ID)中选择时,它必须将请求输入数据库并返回所有客户字段。然后它必须自动填充一些输入。我尝试制作 ajax+jQuery。阿贾克斯不错。它现在正在工作!
这是JS:
我想通了:
<script>
$(document).ready(function() {
$('#customer_load').change(function() {
$.ajax({
url: '<?= $this->url(array('action' => 'ajax', 'controller' => 'baza')) ?>',
type: 'POST',
dataType: 'json',
data: {
// list of request parameters
'customer_id': $(this).attr('value')
},
success: function(data) {
//alert(data.current_discount);
$('#extra_discount').val(data.extra_discount);
$('#current_discount').val(data.current_discount);
$('#customer_number').val(data.customer_id);
}
});
});
});
PHP 初始化:
$this->_helper->AjaxContext()->addActionContext('add', 'json')->initContext('json');
阿贾克斯动作:
$id= $this->_getParam('customer_id');
$result = $this->_customers->fetchSelected($id);
$this->view->customers = $result;
$this->_helper->json($result);
html:
<select name="customer_id" id="customer_load" style="width:300px;">
<option value="0">Выберите заказчика</option>
?php foreach ($this->customers as $cus): ?>
<option value="<?= $cus['customer_id'] ?>"" <?php if ($cus['customer_id'] == $this->form_data['customer_id']) echo "selected"; ?> ><?= $cus['lastname'] . " " . $cus['name'] ?></option>
<?php endforeach; ?>
<option value="new" onclick="NewCustomer()">Новый заказчик</option>
</select>