Task: when I select the client select tag (I have client_id), it should receive a request in the database and return all the client fields. And then it should automatically fill in some data. I am trying to do ajax + jQuery. Ajax is good. He is working now!
Here's JS:
I understand it:
<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 init:
$this->_helper->AjaxContext()->addActionContext('add', 'json')->initContext('json');
Ajax action:
$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>
source share