I am trying to auto fill defaults on my invoices form with customer data based off the customer selected in the drop down. Once the customer is selected in this drop down in the photo below I want it to auto fill the addresses from the data found in the customer table in the next photo below also.
So far this is my invoices add.ctp script to try fetch the data. Currently with this script I can fetch the customer id that was selected to use in a sql query and also set data in the forms inputs
<script>
document.getElementById('customers').addEventListener('change',function(){
alert(this.value);
$('#customers').click(function(){
$.ajax({
type: "POST",
url: '<?php echo Router::url(array('controller' => 'Customers', 'action' => 'fill')); ?>',
success: function(data){
alert(data);
}
});
});
document.getElementById('name').value = "test"
document.getElementById('invoice_to_address').value = "test"
});
</script>
This is my fill function in my CustomersController. This is where I am going wrong I think my query is probably completely wrong and it's not searching for the right thing and returning it incorrectly and probably not even using the customer ID from the form. Currently it just returns no data but also has no errors and an empty alert box comes up due to the script in the view.
public function fill()
{
$layout = 'ajax'; // you need to have a no html page, only the data.
$this->autoRender = false; // no need to render the page, just plain data.
$data = array();
$id = $this->request->data();
$query = $this->Customers->find()
->where([
'id' => $id]);
$this->set(array(
'id' => $query,
'_serialize' => 'id'
));
}
UPDATE
My full Invoices add.ctp
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<?php echo $this->Html->script('jquery.chained'); ?>
<script type="text/javascript">
$(document).ready(function () {
$("#contacts").chained("#customers");
});
</script>
<?php use Cake\Routing\Router; ?>
<?php
/**
* @var \App\View\AppView $this
*/
?>
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Html->link(__('List Invoices'), ['action' => 'index']) ?></li>
<li><?= $this->Html->link(__('List Customers'), ['controller' => 'Customers', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Customer'), ['controller' => 'Customers', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Customer Contacts'), ['controller' => 'CustomerContacts', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Customer Contact'), ['controller' => 'CustomerContacts', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Aircraft Registrations'), ['controller' => 'AircraftRegistrations', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Aircraft Registration'), ['controller' => 'AircraftRegistrations', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Shipping Companies'), ['controller' => 'ShippingCompanies', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Shipping Company'), ['controller' => 'ShippingCompanies', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Job Types'), ['controller' => 'JobTypes', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Job Type'), ['controller' => 'JobTypes', 'action' => 'add']) ?></li>
<li><?= $this->Html->link(__('List Currencies'), ['controller' => 'Currencies', 'action' => 'index']) ?></li>
<li><?= $this->Html->link(__('New Currency'), ['controller' => 'Currencies', 'action' => 'add']) ?></li>
</ul>
</nav>
<div class="invoices form large-9 medium-8 columns content">
<?= $this->Form->create($invoice) ?>
<fieldset>
<legend><?= __('Add Invoice') ?></legend>
<?php
echo $this->Form->input('start_date', ['empty' => false]);
echo $this->Form->input('close_date', ['empty' => true]);
echo $this->Form->input('customer_id', ['options' => $customers, 'empty' => true,'id'=>'customers']);
echo $this->Form->input('name', ['type' => 'text', 'id'=>'name']);
echo $this->Form->input('invoice_to_address', ['type' => 'text', 'id'=>'invoice_to_address']);
echo $this->Form->input('ship_to_address');
echo $this->Form->input('customer_contact_id', ['options' => $customerContacts, 'empty' => true,'id'=>'contacts']);
echo $this->Form->input('currency_id', ['options' => $currencies, 'default'=> 1, 'id'=>'currencies']);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
<?= $this->Form->create(Null, ['type' => 'POST']) ?>
<?= $this->Form->end() ?>
</div>
<script>
jQuery('#name').autocomplete({source:'<?php echo Router::url(array('controller' => 'Customers', 'action' => 'search')); ?>'
});
</script>
<script>
document.getElementById('customers').addEventListener('change',function(){
var id = this.value;
alert(id);
var csrfToken = $('[name=_csrfToken]').val();
$.ajax({
type: "POST",
url: '<?php echo Router::url(array("controller" => "Customers", "action" => "fill")); ?>',
data: {'id' : id},
beforeSend: function(xhr){
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
},
success: function(data){
alert(data);
data = JSON.parse(data);
alert("id: " + data.id);
}
});
document.getElementById('name').value = "test"
document.getElementById('invoice_to_address').value = "test"
document.getElementById('currencies').value = 3;
});
</script>
and the controller fill function
public function fill(){
$layout = 'ajax'; // you need to have a no html page, only the data.
$this->autoRender = false; // no need to render the page, just plain data.
if ($this->request->is('ajax')) {
$id = $this->request->data['id'];
$query = $this->Customers->find()
->where([
'id' => $id
])->first();
echo json_encode($query);
}
}
What happens when ($this->request->is('ajax')) is there. There's no response or preview.
When if ($this->request->is('ajax')) is removed.
Here is the full error message
{"id":0,"name":"Sky Works","country_id":1,"city_id":6,"address":"Sky works address in the customers table","postal_address":"Sky works shippng address in the customers table","phone":"","email":"","payment_terms_id":1,"stop_credit":false,"gst_percentage":null,"currency_id":"2","account_closed":false,"invoice_email":"","customer_notes":""}<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8332d-trace').style.display = (document.getElementById('cakeErr5a1140bb8332d-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (512)</a>: Unable to emit headers. Headers sent in file=/home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php line=179 [<b>CORE/src/Http/ResponseEmitter.php</b>, line <b>48</b>]<div id="cakeErr5a1140bb8332d-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8332d-code').style.display = (document.getElementById('cakeErr5a1140bb8332d-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8332d-context').style.display = (document.getElementById('cakeErr5a1140bb8332d-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5a1140bb8332d-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> $message </span><span style="color: #007700">= </span><span style="color: #DD0000">"Unable to emit headers. Headers sent in file=</span><span style="color: #0000BB">$file</span><span style="color: #DD0000"> line=</span><span style="color: #0000BB">$line</span><span style="color: #DD0000">"</span><span style="color: #007700">;</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">if (</span><span style="color: #0000BB">Configure</span><span style="color: #007700">::</span><span style="color: #0000BB">read</span><span style="color: #007700">(</span><span style="color: #DD0000">'debug'</span><span style="color: #007700">)) {</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB"> trigger_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$message</span><span style="color: #007700">, </span><span style="color: #0000BB">E_USER_WARNING</span><span style="color: #007700">);</span></span></code></pre><pre id="cakeErr5a1140bb8332d-context" class="cake-context" style="display: none;">$response = object(Zend\Diactoros\Response) {
[protected] headers => [
'Content-Type' => [
[maximum depth reached]
]
]
[protected] headerNames => [
'content-type' => 'Content-Type'
]
[private] phrases => [
(int) 100 => 'Continue',
(int) 101 => 'Switching Protocols',
(int) 102 => 'Processing',
(int) 200 => 'OK',
(int) 201 => 'Created',
(int) 202 => 'Accepted',
(int) 203 => 'Non-Authoritative Information',
(int) 204 => 'No Content',
(int) 205 => 'Reset Content',
(int) 206 => 'Partial Content',
(int) 207 => 'Multi-status',
(int) 208 => 'Already Reported',
(int) 226 => 'IM used',
(int) 300 => 'Multiple Choices',
(int) 301 => 'Moved Permanently',
(int) 302 => 'Found',
(int) 303 => 'See Other',
(int) 304 => 'Not Modified',
(int) 305 => 'Use Proxy',
(int) 306 => 'Switch Proxy',
(int) 307 => 'Temporary Redirect',
(int) 308 => 'Permanent Redirect',
(int) 400 => 'Bad Request',
(int) 401 => 'Unauthorized',
(int) 402 => 'Payment Required',
(int) 403 => 'Forbidden',
(int) 404 => 'Not Found',
(int) 405 => 'Method Not Allowed',
(int) 406 => 'Not Acceptable',
(int) 407 => 'Proxy Authentication Required',
(int) 408 => 'Request Time-out',
(int) 409 => 'Conflict',
(int) 410 => 'Gone',
(int) 411 => 'Length Required',
(int) 412 => 'Precondition Failed',
(int) 413 => 'Request Entity Too Large',
(int) 414 => 'Request-URI Too Large',
(int) 415 => 'Unsupported Media Type',
(int) 416 => 'Requested range not satisfiable',
(int) 417 => 'Expectation Failed',
(int) 418 => 'I'm a teapot',
(int) 421 => 'Misdirected Request',
(int) 422 => 'Unprocessable Entity',
(int) 423 => 'Locked',
(int) 424 => 'Failed Dependency',
(int) 425 => 'Unordered Collection',
(int) 426 => 'Upgrade Required',
(int) 428 => 'Precondition Required',
(int) 429 => 'Too Many Requests',
(int) 431 => 'Request Header Fields Too Large',
(int) 444 => 'Connection Closed Without Response',
(int) 451 => 'Unavailable For Legal Reasons',
(int) 499 => 'Client Closed Request',
(int) 500 => 'Internal Server Error',
(int) 501 => 'Not Implemented',
(int) 502 => 'Bad Gateway',
(int) 503 => 'Service Unavailable',
(int) 504 => 'Gateway Time-out',
(int) 505 => 'HTTP Version not supported',
(int) 506 => 'Variant Also Negotiates',
(int) 507 => 'Insufficient Storage',
(int) 508 => 'Loop Detected',
(int) 510 => 'Not Extended',
(int) 511 => 'Network Authentication Required',
(int) 599 => 'Network Connect Timeout Error'
]
[private] reasonPhrase => ''
[private] statusCode => (int) 200
[private] protocol => '1.1'
[private] stream => object(Zend\Diactoros\Stream) {}
}
$maxBufferLength = (int) 8192
$file = '/home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php'
$line = (int) 179
$message = 'Unable to emit headers. Headers sent in file=/home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php line=179'</pre><pre class="stack-trace">Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 48
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 116
[main] - ROOT/webroot/index.php, line 37</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8392c-trace').style.display = (document.getElementById('cakeErr5a1140bb8392c-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Cannot modify header information - headers already sent by (output started at /home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php:179) [<b>CORE/src/Http/ResponseEmitter.php</b>, line <b>146</b>]<div id="cakeErr5a1140bb8392c-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8392c-code').style.display = (document.getElementById('cakeErr5a1140bb8392c-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb8392c-context').style.display = (document.getElementById('cakeErr5a1140bb8392c-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5a1140bb8392c-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> $response</span><span style="color: #007700">-></span><span style="color: #0000BB">getStatusCode</span><span style="color: #007700">(),</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">(</span><span style="color: #0000BB">$reasonPhrase </span><span style="color: #007700">? </span><span style="color: #DD0000">' ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$reasonPhrase </span><span style="color: #007700">: </span><span style="color: #DD0000">''</span><span style="color: #007700">)</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">));</span></span></code></pre><pre id="cakeErr5a1140bb8392c-context" class="cake-context" style="display: none;">$response = object(Zend\Diactoros\Response) {
[protected] headers => [
'Content-Type' => [
[maximum depth reached]
]
]
[protected] headerNames => [
'content-type' => 'Content-Type'
]
[private] phrases => [
(int) 100 => 'Continue',
(int) 101 => 'Switching Protocols',
(int) 102 => 'Processing',
(int) 200 => 'OK',
(int) 201 => 'Created',
(int) 202 => 'Accepted',
(int) 203 => 'Non-Authoritative Information',
(int) 204 => 'No Content',
(int) 205 => 'Reset Content',
(int) 206 => 'Partial Content',
(int) 207 => 'Multi-status',
(int) 208 => 'Already Reported',
(int) 226 => 'IM used',
(int) 300 => 'Multiple Choices',
(int) 301 => 'Moved Permanently',
(int) 302 => 'Found',
(int) 303 => 'See Other',
(int) 304 => 'Not Modified',
(int) 305 => 'Use Proxy',
(int) 306 => 'Switch Proxy',
(int) 307 => 'Temporary Redirect',
(int) 308 => 'Permanent Redirect',
(int) 400 => 'Bad Request',
(int) 401 => 'Unauthorized',
(int) 402 => 'Payment Required',
(int) 403 => 'Forbidden',
(int) 404 => 'Not Found',
(int) 405 => 'Method Not Allowed',
(int) 406 => 'Not Acceptable',
(int) 407 => 'Proxy Authentication Required',
(int) 408 => 'Request Time-out',
(int) 409 => 'Conflict',
(int) 410 => 'Gone',
(int) 411 => 'Length Required',
(int) 412 => 'Precondition Failed',
(int) 413 => 'Request Entity Too Large',
(int) 414 => 'Request-URI Too Large',
(int) 415 => 'Unsupported Media Type',
(int) 416 => 'Requested range not satisfiable',
(int) 417 => 'Expectation Failed',
(int) 418 => 'I'm a teapot',
(int) 421 => 'Misdirected Request',
(int) 422 => 'Unprocessable Entity',
(int) 423 => 'Locked',
(int) 424 => 'Failed Dependency',
(int) 425 => 'Unordered Collection',
(int) 426 => 'Upgrade Required',
(int) 428 => 'Precondition Required',
(int) 429 => 'Too Many Requests',
(int) 431 => 'Request Header Fields Too Large',
(int) 444 => 'Connection Closed Without Response',
(int) 451 => 'Unavailable For Legal Reasons',
(int) 499 => 'Client Closed Request',
(int) 500 => 'Internal Server Error',
(int) 501 => 'Not Implemented',
(int) 502 => 'Bad Gateway',
(int) 503 => 'Service Unavailable',
(int) 504 => 'Gateway Time-out',
(int) 505 => 'HTTP Version not supported',
(int) 506 => 'Variant Also Negotiates',
(int) 507 => 'Insufficient Storage',
(int) 508 => 'Loop Detected',
(int) 510 => 'Not Extended',
(int) 511 => 'Network Authentication Required',
(int) 599 => 'Network Connect Timeout Error'
]
[private] reasonPhrase => 'OK'
[private] statusCode => (int) 200
[private] protocol => '1.1'
[private] stream => object(Zend\Diactoros\Stream) {}
}
$reasonPhrase = 'OK'</pre><pre class="stack-trace">header - [internal], line ??
Cake\Http\ResponseEmitter::emitStatusLine() - CORE/src/Http/ResponseEmitter.php, line 146
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 54
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 116
[main] - ROOT/webroot/index.php, line 37</pre></div></pre><pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb83ee9-trace').style.display = (document.getElementById('cakeErr5a1140bb83ee9-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: Cannot modify header information - headers already sent by (output started at /home/southpac/southpacificavionics.com/team/src/Controller/CustomersController.php:179) [<b>CORE/src/Http/ResponseEmitter.php</b>, line <b>173</b>]<div id="cakeErr5a1140bb83ee9-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb83ee9-code').style.display = (document.getElementById('cakeErr5a1140bb83ee9-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5a1140bb83ee9-context').style.display = (document.getElementById('cakeErr5a1140bb83ee9-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5a1140bb83ee9-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB"> $name</span><span style="color: #007700">,</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB"> $value</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB"> </span><span style="color: #007700">), </span><span style="color: #0000BB">$first</span><span style="color: #007700">);</span></span></code></pre><pre id="cakeErr5a1140bb83ee9-context" class="cake-context" style="display: none;">$response = object(Zend\Diactoros\Response) {
[protected] headers => [
'Content-Type' => [
[maximum depth reached]
]
]
[protected] headerNames => [
'content-type' => 'Content-Type'
]
[private] phrases => [
(int) 100 => 'Continue',
(int) 101 => 'Switching Protocols',
(int) 102 => 'Processing',
(int) 200 => 'OK',
(int) 201 => 'Created',
(int) 202 => 'Accepted',
(int) 203 => 'Non-Authoritative Information',
(int) 204 => 'No Content',
(int) 205 => 'Reset Content',
(int) 206 => 'Partial Content',
(int) 207 => 'Multi-status',
(int) 208 => 'Already Reported',
(int) 226 => 'IM used',
(int) 300 => 'Multiple Choices',
(int) 301 => 'Moved Permanently',
(int) 302 => 'Found',
(int) 303 => 'See Other',
(int) 304 => 'Not Modified',
(int) 305 => 'Use Proxy',
(int) 306 => 'Switch Proxy',
(int) 307 => 'Temporary Redirect',
(int) 308 => 'Permanent Redirect',
(int) 400 => 'Bad Request',
(int) 401 => 'Unauthorized',
(int) 402 => 'Payment Required',
(int) 403 => 'Forbidden',
(int) 404 => 'Not Found',
(int) 405 => 'Method Not Allowed',
(int) 406 => 'Not Acceptable',
(int) 407 => 'Proxy Authentication Required',
(int) 408 => 'Request Time-out',
(int) 409 => 'Conflict',
(int) 410 => 'Gone',
(int) 411 => 'Length Required',
(int) 412 => 'Precondition Failed',
(int) 413 => 'Request Entity Too Large',
(int) 414 => 'Request-URI Too Large',
(int) 415 => 'Unsupported Media Type',
(int) 416 => 'Requested range not satisfiable',
(int) 417 => 'Expectation Failed',
(int) 418 => 'I'm a teapot',
(int) 421 => 'Misdirected Request',
(int) 422 => 'Unprocessable Entity',
(int) 423 => 'Locked',
(int) 424 => 'Failed Dependency',
(int) 425 => 'Unordered Collection',
(int) 426 => 'Upgrade Required',
(int) 428 => 'Precondition Required',
(int) 429 => 'Too Many Requests',
(int) 431 => 'Request Header Fields Too Large',
(int) 444 => 'Connection Closed Without Response',
(int) 451 => 'Unavailable For Legal Reasons',
(int) 499 => 'Client Closed Request',
(int) 500 => 'Internal Server Error',
(int) 501 => 'Not Implemented',
(int) 502 => 'Bad Gateway',
(int) 503 => 'Service Unavailable',
(int) 504 => 'Gateway Time-out',
(int) 505 => 'HTTP Version not supported',
(int) 506 => 'Variant Also Negotiates',
(int) 507 => 'Insufficient Storage',
(int) 508 => 'Loop Detected',
(int) 510 => 'Not Extended',
(int) 511 => 'Network Authentication Required',
(int) 599 => 'Network Connect Timeout Error'
]
[private] reasonPhrase => 'OK'
[private] statusCode => (int) 200
[private] protocol => '1.1'
[private] stream => object(Zend\Diactoros\Stream) {}
}
$name = 'Content-Type'
$values = [
(int) 0 => 'text/html; charset=UTF-8'
]
$first = true
$value = 'text/html; charset=UTF-8'</pre><pre class="stack-trace">header - [internal], line ??
Cake\Http\ResponseEmitter::emitHeaders() - CORE/src/Http/ResponseEmitter.php, line 173
Cake\Http\ResponseEmitter::emit() - CORE/src/Http/ResponseEmitter.php, line 55
Cake\Http\Server::emit() - CORE/src/Http/Server.php, line 116
[main] - ROOT/webroot/index.php, line 37</pre></div></pre>