Reputation: 328
In the Silverstripe website, the user submits a form and it gets stored on the CMS for content managers to see the submitted forms.
The functionality works, but the problem is that I have $data
returned in a PHP array and I want to output it to the CMS.
The only way I figured out is to convert it to JSON, but then it just outputs a JSON string, and I would like to have something like HTML table to make it more human readable. How would I do this?
My code so far is:
// converts array to jason, on controller
$SubmitedResult->SerialisedForm = Convert::array2json($data);
// $db on dataobject
private static $db = array(
'SerialisedForm' => 'Text',
);
// JSON string received below
{"url":"\/test\/test-test\/testSubmit","Name":"Tom","Email":"[email protected]","Phone":"564456","SecurityID":"c5efe841e26d6d088dd94dfcfe76f6ec80acac86","action_submit":"Submit"}
Upvotes: 1
Views: 256
Reputation: 5875
Usually you'd want to build a DataObject
where you store your submitted form data. It seems like you already have that, but you use it to store all the data in a field called SerialisedForm
. I suggest you create a separate field for all your form fields instead.
Example:
class FormSubmission extends DataObject
{
private static $db = [
'Name' => 'Varchar(255)',
'Email' => 'Varchar(255)',
'Phone' => 'Varchar(64)'
];
// The summary_fields ensure that your fields directly show up in the GridField
private static $summary_fields = [
'Name' => 'Name',
'Email' => 'Email',
'Phone' => 'Phone'
];
}
Then in your form submit handler, you do:
public function testSubmit($data, $form)
{
$submittedResult = FormSubmission::create();
$form->saveInto($submittedResult);
$submittedResult->write();
// redirect back or somewhere else…
}
In the CMS you can use a GridField
on your Page or a ModelAdmin
instance to view the submissions.
Upvotes: 3