The form helper should reflect the updated data after post. This works perfectly after a regular http post, but not with ajax post and I have no idea why.
Any ideas?
$this->request->data[‘key’] = 100 works, but this is a bad habbit and deprecated in 3.6 at all.
Ok, Ive looked into EntityContext and it seems the requestData has priority over the enttity data (requestData returns first place in the val() method).
So the updated entity value will not be shown in the form. Modifying the requestData is bad practice and deprecated.
So what now? Update every input control with a value="" attribute and a “custom” array? Can’t believe there is no other way…
After post via ajax. Some input is based on calculation and the result should be updated to the form field after ajax submit. It does work on a “regular” post but not with ajax.
How you return result for ajax in controller?
If you return JSON format, so you can render json_encode(Array);
The json_encode result can read after ajax success.
This example code just idea not real code.
AJAX
$.ajax({
url : '../brands/add',
data : {
name : "test",
shortname : "tst"
},
dataType : 'json',
success : function(data) {
console.log(data);
//here you can write update form
},
error : function(data) {
console.log("Eorror occured");
}
});
Controller
public function add() {
$this->autoRender = false;
if (this->request->is('ajax') && this->request->is('post') ){
$res = [
'data' => [
/* your data */
]
];
$this->response->body(json_encode($res));
return $this->response;
}
}
Like rrd said, if you get the ajax response or the returned value of your controller, you should be able to manipulate the DOM object using javascript. With that said, I don’t think is a CakePHP issue.
Now to pass the result of your logic from your controller you have to set the variables first and then echo encode those variables to get them ready for your javascript.