PHP code example of smallbearsoft / yii2-ajax

1. Go to this page and download the library: Download smallbearsoft/yii2-ajax library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

smallbearsoft / yii2-ajax example snippets




namespace app\controllers;

use Yii;
use yii\web\Controller;

public function actionLink()
{
    return $this->render('link');
}

public function actionResponse()
{
    return 'Success, this is your data.';
}



use smallbearsoft\ajax\Ajax;
use yii\web\JsExpression;
use yii\helpers\Url;

Ajax::begin(['clientOptions' => [
    'success' => new JsExpression('function(data, textStatus, jqXHR) {alert(data)}'),
    'error' => new JsExpression('function(jqXHR, textStatus, errorThrown) {alert(errorThrown)}'),
    'beforeSend' => new JsExpression('function(jqXHR, settings) {alert("Before send.")}'),
    'complete' => new JsExpression('function(jqXHR, textStatus) {alert("Complete.")}'),
    'timeout' => 10000
]]) 



namespace app\controllers;

use Yii;
use yii\web\Controller;

public function actionForm()
{
    return $this->render('form');
}

public function actionPost()
{
    if(isset($_POST['name']) && isset($_POST['age'])) {
        $name = $_POST['name'];
        $age = $_POST['age'];
        return "Success, name is $name and age is $age.";
    } else {
        return 'Success, but we can not get the name and age.';
    }
}



use smallbearsoft\ajax\Ajax;
use yii\web\JsExpression;
use yii\helpers\Url;

Ajax::begin(['clientOptions' => [
    'success' => new JsExpression('function(data, textStatus, jqXHR) {alert(data)}'),
    'error' => new JsExpression('function(jqXHR, textStatus, errorThrown) {alert(errorThrown)}'),
    'beforeSend' => new JsExpression('function(jqXHR, settings) {alert("Before send.")}'),
    'complete' => new JsExpression('function(jqXHR, textStatus) {alert("Complete.")}'),
    'timeout' => 10000
]])