2017 © Pedro Peláez
 

yii2-extension yii2-ajax

This is a ajax widget of yii2 which will generate a div. The link click or form submission (for those link and form with data-ajax attribute) in this div will trigger an AJAX request.

image

smallbearsoft/yii2-ajax

This is a ajax widget of yii2 which will generate a div. The link click or form submission (for those link and form with data-ajax attribute) in this div will trigger an AJAX request.

  • Thursday, October 15, 2015
  • by smallbearsoft
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,092 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 5 Versions
  • 26 % Grown

The README.md

yii2-ajax

This is a ajax widget of yii2 which will generate a div. The tags (except form) click or forms submission (for those tags and forms with data-ajax attribute) in this div will trigger an ajax request., (*1)

We can get some data by ajax link useing Ajax widget. First, we should have a controller and an action to render our view. Of course, we can just use SiteController as our controller. Then we write an action in SiteController named actionLink to render link.php. In addition, add actionResponse action to response ajax request through ajax link:, (*2)

SiteController.php, (*3)

render('link');
}

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

link.php, (*4)

 [
    '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
]]) ?>
    <a href="<?= Url::to(['site/response']) ?>" data-ajax="1">This is an ajax link.</a>

Simple example for form

If you want to use ajax to post a form to server, you can use this Ajax widget make it essay. We will still use SiteController as our controller. Then we add two actions actionForm and actionPost. To make it simple, we will not use ActiveForm widget, but you can use that in your code:, (*5)

SiteController.php, (*6)

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.';
    }
}
?>

form.php, (*7)

 [
    '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
]]) ?>
    <form action="<?= Url::to(['site/post'])?>" method="post" data-ajax="1">
        <input type="text" name="name" value="Fangxin Jiang"/>
        <input type="text" name="age" value="22"/>
        <input type="submit" value="Submit"/>
    </form>

Actually you can also use Ajax widget to upload files, just add an input like <input type="file" name="image"/>. You will see your file in $_FILES variable on server., (*8)

More tutorials

See Home wiki for more tutorials., (*9)

Tips

To trigger the Ajax widget, you can use a submit button or a jquery submit() function. But cannot use the pure javascript submit() function to trigger it. You can use:, (*10)

<button type="submit">Submit</button> (In your form)
---OR---
$("#my-id").submit()

Cannot use:, (*11)

document.getElementById("#my-id").submit()

Installation

The preferred way to install this extension is through composer., (*12)

Either run, (*13)

composer require smallbearsoft/yii2-ajax:*

or add, (*14)

"smallbearsoft/yii2-ajax": "*"

to the require section of your composer.json file., (*15)

The Versions

15/10 2015

dev-master

9999999-dev

This is a ajax widget of yii2 which will generate a div. The link click or form submission (for those link and form with data-ajax attribute) in this div will trigger an AJAX request.

  Sources   Download

MIT

The Requires

 

by Fangxin Jiang

extension ajax widget yii2-ajax

15/10 2015

1.0.2

1.0.2.0

This is a ajax widget of yii2 which will generate a div. The link click or form submission (for those link and form with data-ajax attribute) in this div will trigger an AJAX request.

  Sources   Download

MIT

The Requires

 

by Fangxin Jiang

extension ajax widget yii2-ajax

09/09 2015

1.0.1

1.0.1.0

This is a ajax widget of yii2 which will generate a div. The link click or form submission (for those link and form with data-ajax attribute) in this div will trigger an AJAX request.

  Sources   Download

MIT

The Requires

 

by Fangxin Jiang

extension ajax widget yii2-ajax

08/09 2015

1.0.0

1.0.0.0

This is a ajax widget of yii2 which will generate a div. The link click or form submission (for those link and form with data-ajax attribute) in this div will trigger an AJAX request.

  Sources   Download

MIT

The Requires

 

by Fangxin Jiang

extension ajax widget yii2-ajax

06/09 2015

0.1.0

0.1.0.0

This is a ajax widget of yii2 which will generate a div. The link click or form submission (for those link and form with data-ajax attribute) in this div will trigger an AJAX request.

  Sources   Download

MIT

The Requires

 

by Fangxin Jiang

extension ajax widget yii2-ajax