Yii 2 Response formats: RAW, HTML, JSON, JSONP, XML

If you want that your method return data in specific format in Yii2 then you can use this simple construction:
Example method return in JSON format:

use yii\web\Response;
//......
public function actionJson()
    {
        $something = true; // or you can set for test -> false;
        $return_json = ['status' => 'error'];
        if ($something == true)
        {
            $return_json = ['status' => 'success', 'message' => ' is successfully saved'];
        }
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return $return_json;
    }

/*header results
Connection: Keep-Alive
Content-Length: 55
Content-Type : application/json; charset=UTF-8
*/

Continue reading Yii 2 Response formats: RAW, HTML, JSON, JSONP, XML

How to create HTML select lists in Joomla!

If you need create some HTML select list with items then you can use Joomla API.
Joomla! provide special class JHTML for creating form elements, such as selectlist, radiolist, booleanlist and etc…
Most popular. Get data from database and create selectlist.
Example ↓

$selected = JFactory::getInt('selectlist_name', 0); 

$db = JFactory::getDBO();
    $query = "SELECT `id` AS `value`, `title` AS `text` FROM `#__your_table` WHERE `cid`=1";
$db->setQuery($query);
$items_from_db = $db->loadObjectList();

$selectlist_data[] = JHTML::_('select.option',  '0', "- Select item -", 'value', 'text' );
$selectlist_data = array_merge( $selectlist_data, $items_from_db);

$list = JHTML::_( 'select.genericlist',
                    $selectlist_data, 
                    'selectlist_name', 
                    'size="1" class="inputbox selectlist"', 
                    'value', 'text', 
                    $selected, 'selectlist_id', 
                    true
);
echo $list;

Result ↓

How to set right page encoding using PHP (UTF-8 HTTP header)?

Quite frequently, many are faced with the problem: incorrect definition of the browser page coding.
The easy way to fix it – set the right page encoding by PHP.
Example (we use the encoding utf-8) ↓

<?php
header('Content-Type: text/html; charset=utf-8');
/*....*/
?>

Additionally, you can specify the encoding using HTML
Continue reading How to set right page encoding using PHP (UTF-8 HTTP header)?

Parameter in XML form structure field for allowing HTML tags

When you are using XML form structure you can use editor form field type.
In that case for allowing HTML tags you have to use the attribute:

filter=’safehtml’

to allow saving the content with html tags.
Example ↓

<field
name="field_desc"
type="editor"
filter="safehtml"
width="350"
height="80"
label="COM_YOURCOMPONENT_EDITOR_FIELD_LABEL"
description="COM_YOURCOMPONENT_EDITOR_FIELD_DESC"
cols="20"
rows="3"
class=""
default=""
/>

Continue reading Parameter in XML form structure field for allowing HTML tags

Allow HTML code in JRequest

use html

If you need to get HTML code from JRequest then you need to use special parameter:

JREQUEST_ALLOWHTML

Example (Joomla! 1.6.x-3.x) ↓

$value = JRequest::getVar( 'field_name', '', 'post', 'string', JREQUEST_ALLOWHTML );

Note ↓

      JREQUEST_NOTRIM – prevents trimming of whitespace
      JREQUEST_ALLOWRAW – bypasses filtering
      JREQUEST_ALLOWHTML – allows most HTML. If this is not passed in, HTML is stripped out by default.

Continue reading Allow HTML code in JRequest