簡體   English   中英

如何從控制器中調用Ajax函數...在Codeigniter中

[英]how to call ajax function from controller…in codeigniter

我需要在codeigniter中創建國家/地區下拉列表。 onchange事件即時消息調用了另一個項目名稱為ajax.php的控制器,我需要知道如何在codeigniter中獲取url並將數據發送到url。

我的ajax功能是

var base_url = "<? echo base_url()?>";

        function getstate(value) {

                if (value !== '') {

                  //alert('test');

                    $.ajax({
                        type: "POST",
                        url:base_url+"adminzone/ajax/ajax.php",
                        data: "do=getstate&value=" + value,
                        success: function(msg) {

                            alert(msg);

                            //$('#psid').html("<img src='images/spacer.gif'>");

                            $('#reg1').html(msg);


                            //

                            //$('#sid').sSelect({ddMaxHeight: '300px'});

                        },
                        error: function() {
                            //alert('some error has occured...');
                        },
                        start: function() {
                            //alert('ajax has been started...');    
                        }
                    });
                }
            }

我的ajax控制器是

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

//error_reporting(0); class ajax extends CI_Controller {

    public function __construct() {
        parent::__construct();

        if (!$this->session->userdata('admin_logged_in')) {

            redirect('adminzone');
        }
        $this->load->model('doctor_model');
    }

    public function getstate(){
        echo $this->input->post();exit;
    }
     }

視圖中的ajax函數

  $('#countryfield').change(function() {
        var passedvalue = $('#countryfield').val();
        var path = base_url+"ajax/getState";
        $.ajax({
            type: "POST",
            url: path,
            data: {'passedvalue': passedvalue},
            success: function(data) {
                if (data) {
                    alert(success);//task done on success
                }
            },
            error: function() {
                    alert('some error occurred');
                },
          });
      })

現在您可以在ajax.php控制器中編寫函數。 函數名稱應為getState

     public function getstate(){
       echo $this->input->post('passedvalue'); //now here you can perform your task
         exit;
       }

現在,您可以在控制器中執行任務,並回顯要傳遞給視圖的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM