Sto*_*per 6 php ajax jquery post zend-framework
任务:当我从select tag customer(我有customer_id)中选择时,它必须得到DB的请求并返回所有客户字段.然后它必须自动填充一些输入.我尝试制作ajax + jQuery.Ajax很好.它现在正在工作!
这是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);
}
});
});
});
Run Code Online (Sandbox Code Playgroud)
PHP初始化:
$this->_helper->AjaxContext()->addActionContext('add', 'json')->initContext('json');
Run Code Online (Sandbox Code Playgroud)
Ajax动作:
$id= $this->_getParam('customer_id');
$result = $this->_customers->fetchSelected($id);
$this->view->customers = $result;
$this->_helper->json($result);
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
从您的帖子中很难理解问题是在客户端还是服务器端......在您的第一个示例中,您没有customer_id
在ajax请求中使用,并且不需要将值转换为Number
javascript。
使用下面的 AJAX 请求:
$(document).ready(function(){
$.ajax({
url: <?= $this->url(array('action' => 'add', 'controller' => 'baza')) ?>,
type: 'POST',
dataType: 'json',
data: {
// list of request parameters
'customer_id': $('select[name=customer_id] option:selected').val(),
},
success: function(results){
// analyze your response and add custom logic
console.debug(result);
}
});
});
Run Code Online (Sandbox Code Playgroud)
根据您的 PHP 代码,您使事情变得过于复杂。在操作顶部添加检查,并在尝试使其工作时将其注释掉(这样您可以baza/add
直接在浏览器中进行测试),一旦工作正常,请取消注释并进行测试。使用JSON 视图助手输出 json。
public function addAction()
{
// checks/validation/etc
// do some processing...
$result = $this->_customers->fetchSelected($id);
// Send the JSON response:
$this->_helper->json($result);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
254 次 |
最近记录: |