Yii2 method of automatically obtaining form data based on Ajax _php instance

Source: Internet
Author: User
Tags smarty template yii

This article gives an example of how YII2 automatically obtains form data based on Ajax. Share to everyone for your reference, specific as follows:

Here are two tables, the table structure is as follows, the locations table stores the province and the postcode and so on information, both tables model and curd both use the GII to generate

Yii2advanced.customers table

Customer_id:int (11)
Customer_name:varchar (100)
Zip_code:varchar (20)
City:varchar (100)
Province:varchar (100)

Yii2advanced.locations table

Location_id:int (11)
Zip_code:varchar (20)
City:varchar (100)
Province:varchar (100)

This will be done by automatically populating the form with city and province information after the customer chooses Zip_Code

Implementation methods

First, you need to add a method to the locations controller, and he can obtain the corresponding location information through the zip_id.

Public Function actiongetcityprovince ($zipId)
{
  $location = Locations::findone ($zipId);
  echo Json::encode ($location);
}

Then through JS listening to select, when select changes, using the jquery get method to obtain the corresponding information, and using the jquery attr method to set the city and province value can

JS code, in the customer's Form view

#zipCode is the ID of the Select

<?php
$script = <<<js
jQuery (' #zipCode '). Change (function () {
  var zipid = $ (this). Val ();
  Jquery.get (' Index.php?r=locations/get-city-province ', {zipid:zipid},function (data) {
    var data = Jquery.parsejson (data);
    JQuery ("#customers-city"). attr ("value", data.city);
    JQuery ("#customers-province"). attr ("value", data.province);
  });
JS;
$this->registerjs ($script);
>

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.