【发布时间】:2012-12-10 23:04:53
【问题描述】:
任务:当我从选择标签客户(我有客户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');
Ajax 动作:
$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>
【问题讨论】:
-
您是否使用某种 mod-rewrite 来解析 Ajax 调用“/add”中的给定 URL?
-
暂时禁用
isXmlHttpRequest签入控制器并首先在浏览器中直接测试您的ajax 操作。一旦你让它工作,添加安全条件并实现你的 ajax 调用。 -
感谢 Alex 的建议,我将 url 更改为 '/baza/add',但在我的萤火虫中它是 POST voskmodel.dev/baza/add 200 OK 65ms 我猜 customer_id 无处发送
-
暂时将
$this->getRequest()->getPost更改为$this->_getParam并在浏览器中这样www.mysite.com/controller/action/customer_id/123请求
标签: php jquery ajax zend-framework post