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
https://forums.phpfreaks.com/topic/251537-help-in-ajax-call/
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
https://forums.phpfreaks.com/topic/251537-help-in-ajax-call/#findComment-1289958
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
https://forums.phpfreaks.com/topic/251537-help-in-ajax-call/#findComment-1290101
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.