Jump to content

Help in Ajax call


vikrantmohite

Recommended Posts

Please help me in this guys.......

I am using MVC model for my application .

 

I am stuck with the ajax call.

 

this is my controller

application/controller/admin/index.php

 

function getCity()

  {

        $city=$this->model->getCity();

 

        foreach($this->city as $key => $value)

        {

            echo '<option>'.$value['name'].'</option>' ;

        }

  }

 

this is my model

application/model/admin_model.php

 

 

 

 

public function getCity()

  {

      if($_POST['id'])

      {

      $cid=$_POST['id'];

      $sth = $this->db->prepare('SELECT id, name FROM unit WHERE cid= :cid');

      $sth->execute(array(':cid' => $cid));

      return $sth->fetchAll();

      }

  }

 

 

this is my view

 

<form id="city" action="<../application/admin/regstr/" method="post">

 

   

  Country

    <select name="country" class="country">

  <option value="" selected="selected">--Select Taluka--</option>

  <option value="1">India</option>

        <option value="2">USA</option>

  <option value="3">China</option>

    </select>

    City

    <select name="city" class="city">

  <option selected="selected">--Select City--</option>

   

    </select>

   

  <input type="submit" />

</form>

 

 

and this is my javascript for Ajax

 

$(document).ready(function()

{

 

  $(".country").change(function()

  {

      var id=$(this).val();

      var dataString = 'id='+ id;

 

      $.ajax

      ({

        type: "POST",

        url: "admin/getCity",

        data: dataString,

        cache: false,

        success: function(html)

        {

            $(".city").html(html);

        }

      });

 

  });

 

});

 

 

 

But I am not getting the output. I have tested it with firebug in response tab. Instead of getting only option values html code as output  I am getting the whole HTML page content (i.e. content of view page admin/index.php.

 

 

Please help me out.

Link to comment
Share on other sites

The path for that controler function is admin/index/getCity. Change your javascript.

 

Thanx for quick reply.

 

I am using MVC So I have to call a function in controller which inturn receive data from model.

 

admin is controller class and getCity is its method. getCity method calls model and receives data from it. So I dont think admin/index/getCity is going to work for me.

 

mu Url is like localhost/application/controller/{method}/{arg if any} structure

Link to comment
Share on other sites

Are you using any framework or did you build app from scratch? It looks to me like CodeIgniter thats why i said to fix the link in JS.

 

What is the name of class inside of admin/index.php controler?

 

 

I am building application from scratch in MVC framework.  I am not using CodeIgniter. I am using controller admin.php.

 

..//application/controllers/admin.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.