Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: AJAX autofill Cakephp 3 form

mark.nz:

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




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

Currently the script for the add.ctp




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":""}
Warning (512): Unable to emit headers. Headers sent in file=/home/southpac/http://ift.tt/2zfxZiv line=179 [CORE/src/Http/ResponseEmitter.php, line 48]
Warning (2): Cannot modify header information - headers already sent by (output started at /home/southpac/http://ift.tt/2zgwq3Y) [CORE/src/Http/ResponseEmitter.php, line 146]
Warning (2): Cannot modify header information - headers already sent by (output started at /home/southpac/http://ift.tt/2zgwq3Y) [CORE/src/Http/ResponseEmitter.php, line 173]




Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: AJAX autofill Cakephp 3 form

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×