John Davidson

php - how to pass json encode data to a form

0 comments
Message:



function PrintMembers(){
var url = '/PrintMList';
var MDataList = $('#MDataList').val();
var form = $('<form action="' + url + '" method="post" id="search" target="_blank">' +
'@csrf'+
'<input type="text" name="MDataList" value="' + MDataList + '" />' +
'<input type="text" name="Action" value="'+Action+'" />' +
'</form>');
$('body').append(form);
$("#search").submit();
}

in the PrintMList page, the array is empty when i print_r the MDataList in controller as below:


$var = $request->input('MDataList');
print_r($var);


My Answer:
To pass JSON encoded data to a form, you can first encode the data using `json_encode` in your PHP code and then pass it as a hidden input field in the form. Here's an example of how you can do this:

1. Encode the data in your PHP code:

php
$MDataList = json_encode($yourDataArray);


2. Pass the encoded data to the form:

javascript
function PrintMembers(){
var url = '/PrintMList';
var MDataList = ;
var form = $('');
$('body').append(form);
$("#search").submit();
}


3. In your controller, decode the JSON data:

php
$var = json_decode($request->input('MDataList'), true);
print_r($var);


This way, you can pass JSON encoded data to a form and retrieve it in your controller.

Rate this post

4 of 5 based on 9746 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us