The example of this article describes the YII2 controller controller Ajax operation method. Share to everyone for your reference, specific as follows:
Public Function Actionsample ()
{
if (Yii:: $app->request->isajax) {
$data = Yii:: $app->request- >post ();
$searchname = Explode (":", $data [' searchname ']);
$searchby = Explode (":", $data [' Searchby ']);
$searchname = $searchname [0];
$searchby = $searchby [0];
$search =//your logic;
\yii:: $app->response->format = \yii\web\response::format_json;
return [
' Search ' => $search,
' code ' =>,
];
}
Template Invoke method
$.ajax ({
URL: ' <?php echo Yii:: $app->request->baseurl. '/supermarkets/sample '?> ',
type: ' Post ',
data: {searchname: $ ("#searchname"). Val (), searchby:$ ("# Searchby "). Val ()},
success:function (data) {
console.log (data.search);
}
);
For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with the PHP program design based on the YII framework.