There is a form. There is a model and a table in the database. There is a controller. HOW to accept and save data Data = $ (this) .serialize (); transmitted by ajax ???

Please help with the controller. Normal post everything works fine. And with Ajax trouble.

    3 answers 3

    If, as I understand it, the problem is in receiving data, and you just cannot see them in $ _POST or $ _REQUEST, then try this technique:

    $request = json_decode(file_get_contents('php://input'), true) 

    If not, specify and I will add the answer

    • The solution was found. But why so - I do not know. Completed the question. Thanks for the answer! - n.osennij

    The simplest example:

     class ExampleController extends Controller { public function actionAjax() { $post = Yii::$app->request->post(); var_dump($post); } } 

    Ajax send data to /example/ajax and get what you sent. Everything should be easy. If this option does not work, then the problem is much deeper. Perhaps some basic modules and components are missing yii.

    • The solution was found. But why so - I do not know. Completed the question. Thanks for the answer! - n.osennij

    Suddenly, a solution was found. I did not understand what was the matter. If you send a request like this (below), then everything is ok - the data comes to the server as an array.

     jQuery('#peoples_form').submit(function (e) { e.preventDefault(); var peopleData=$(this).serialize(); $.ajax({ type: "POST", url: '/site/add-new-people', async: async, timeout: 20000, dataType: datatype, data: peopleData, success: function(result){console.log(result);}, error: function(err){console.log(err);} }); }); 

    But if you send it like this (Ajax is rendered as a function)

     jQuery('#peoples_form').submit(function (e) { e.preventDefault(); var peopleData=$(this).serialize(); aj('/site/add-new-people', {'peopleData': peopleData}); }); function aj(url1, data, async='false', datatype='html') { $.ajax({ type: "POST", url: url1, async: async, timeout: 20000, dataType: datatype, data: data, success: function(result){console.log(result);}, error: function(err){console.log(err);} }); } 

    That data arrives not as an array, but as a string of the form

     _csrf=vk2P3oAnKy855oOydhIt_xcX6ZFuHp1t3AJkGg9eFTlv3-2I0kuToH7bBm3V32uvZFnLhu8-UCkp99pmEv7qug%3D%3D&People%5Bsurname%5D=111&People%5Bfirst_name%5D=&People%5Bmiddle_name%5D=&People%5Bbirth_year%5D=121&People%5Bissue_date%5D=&People%5Bissued_by%5D=&People%5Bpassport_number%5D=111&People%5Bregistration%5D=&People%5Bgender%5D=111&People%5Bclient_note%5D= 

    Which I still could not cram into the model. The problem seems to be solved. But the reason is not clear to me. It can be seen, this is due to the fact that in the way I passed the data to the function as json.

    There is only one oddity left - if you reload the page and submit the form, for some reason, ajax works twice. After that (before rebooting) as it should be - once. It was decided - it’s necessary to hang js not on the submit event, but on('beforeSubmit', 'form#peoples_form', function (e) and add return false at the end on('beforeSubmit', 'form#peoples_form', function (e) ;

    • @kizoso what link? There are no links here - n.osennij
    • It was a standard auto message, meaning that the answer does not contain an answer. You can answer your own question. Transfer the part with the answer from the question here. - 0xdb
    • one
      @ n.osennij aj('/site/add-new-people', peopleData); - that all the difference - Igor