1

I am using Codeigniter framework and MVC structure . My problem is I don't what is to be given to the url in ajax call. This is my ajax call

$.ajax({
  url://--what is to be given here?-- //
  type: "GET",
  data: {
   'leftData': leftData,
   'rigthData': rigthData,
   'func':'editsuccess'
  }
});

the above code I have written in multi_edit.php which is in views folder. The views folder is under application folder and the application folder is under Bunny. So Bunny is my web application name. I want to pass the the values leftData and rigthData to a function in catch() in multi.php which is under libraries. The libraries is under application. So what should be my url. And what should I write on other side to receive those values

Ashoka
  • 452
  • 1
  • 5
  • 20
  • 1
    Writting AJAX backend in views is a wrong concept. You should write it in Controller. – Pupil Dec 26 '13 at 10:29
  • something like `base_url('controller/method');` In JavaScript you want to do [following](http://stackoverflow.com/a/20741717/1564365). (first part of answer and look at url on second part of answer. – Kyslik Dec 26 '13 at 10:29
  • path of the source file (including file with extension) from which you want to request the data. – NiksD Dec 26 '13 at 10:32

8 Answers8

1

You can try it...

var _baseUrl = "<?= base_url() ?>"; //define this part somewhere else like in header_view.php, (something that is on TOP of page the best in <head> tag
var leftData = $(this).data('leftData'); // set this inside of element that is calling this (JavaScript) function for example <span data-leftData="value">test</span>
$.ajax({
  url: _baseUrl + "controller/function/" + elementID, //example
  type: "POST",
  data: {
   'leftData': leftData,
   'rigthData': rigthData,
   'func':'editsuccess'
  }
});
lighter
  • 2,808
  • 3
  • 40
  • 59
  • I would define JavaScript variable for this not use `base_url()` because OP would have to mix `JavaScript` code with `PHP` a lot. – Kyslik Dec 26 '13 at 10:31
  • I don't the values to be sent to controller . I want it in catch function which in mutli.php and multi.ph is in libraries folder. The libraries comes under application folder – Ashoka Dec 26 '13 at 10:33
  • `` anywhere on the top of page and baseUrl is set for every JavaScript that is included after it, I find this as best practise because you dont have to change anything in your JavaScript files when moving to different domain. Send everything in POST and don't bother with (GET) parameters. – Kyslik Dec 26 '13 at 10:35
  • Now I understand, you sent "data" to javascript like this in HTML set `data-whatever="value"` and now in JavaScript side use something simmilar to this to catch data `var elementID = $(this).data('whatever');` – Kyslik Dec 26 '13 at 10:42
  • Ya I know base_url. Thanks so , your tell me to pass the values first to the controller and from there i should pass the value to the place i require it . Is it like that. – Ashoka Dec 26 '13 at 10:44
  • I fixed it. Is this answer you want it? – lighter Dec 26 '13 at 10:51
  • And please use POST instead of GET, LLL I am trying to help out @Ashoka with his problem as 1st priority. I think answer is correct. – Kyslik Dec 26 '13 at 10:56
  • Actually is somewhat like this . leftData is an array var leftData = JSON.stringify(addedLeftValues); – Ashoka Dec 26 '13 at 11:15
0

you can try like this

url:'<?=site_url("yourController/yourFunction")?>'

or if you have set you base_url in config file

url:'<?=base_url("yourController/yourFunction")?>'
Ashok Maharjan
  • 163
  • 1
  • 7
  • how to receive in the function and hoew i would send to the desired place multi.php which is in the libraries folder – Ashoka Dec 26 '13 at 11:28
  • you can load library file in your function of controller `$this->load->library('library_name')` and `$this->library_name->function_name()` to call the function `$this->input->get('variable_name')` or `$_GET['variable_name]` to grab the values – Ashok Maharjan Dec 26 '13 at 11:41
0
url:"www.yourdomain.com/controller_name/function_name/param1/param2",  

Here controller_name is the controller you want to call, function_name is function in that controller and param1 and param2 are parameters you wish to pass(if any).

user2936213
  • 1,021
  • 1
  • 8
  • 19
0

First, you are calling a service. So it should not be under Views. Follow MVC architecture. It should be a function in the controller. Then you can give it as url till the function name like answered before

pratim_b
  • 1,160
  • 10
  • 29
0

the URL is the link to the file you want to send data for exemple

url:"./pages/test.php"
0

First you should call a function inside a controller. By writing url like this.

url:"controller/function"

and from there you redirect to the function in the library called multi.php

$this->load->library('multi',NULL,'myMultiObj');

$this->myMultiObj->somemethod();

Harish Lalwani
  • 754
  • 5
  • 20
0

If you are using ajax code in your view file means then you can follow

$.ajax({
  url: <?php echo base_url('controller/action/params');?>,
  type: "GET",
  data: {
   'leftData': leftData,
   'rigthData': rigthData,
   'func':'editsuccess'
  }
});

If you are using this ajax in your js file means then you should keep your application base url to one variable like

var base_url = "http://localhost/myapp/";
$.ajax({
      url: base_url + 'controller/action/params',
      type: "GET",
      data: {
       'leftData': leftData,
       'rigthData': rigthData,
       'func':'editsuccess'
      }
    });
user2943773
  • 254
  • 1
  • 4
  • 10
0

Try this->

$.ajax({

url:"the path from this file to the file you want to pass the data",

--- rest of the codes goes here---

});

or you can simply pass the data like ->

url: "url?data1=" + data1 + "&data2=" + data2,

and retrieve the data in your multi.php using $_GET global variable