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

3
  • 1
    Writting AJAX backend in views is a wrong concept. You should write it in Controller. Commented Dec 26, 2013 at 10:29
  • something like base_url('controller/method'); In JavaScript you want to do following. (first part of answer and look at url on second part of answer. Commented Dec 26, 2013 at 10:29
  • path of the source file (including file with extension) from which you want to request the data. Commented Dec 26, 2013 at 10:32

8 Answers 8

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' } }); 
Sign up to request clarification or add additional context in comments.

8 Comments

I would define JavaScript variable for this not use base_url() because OP would have to mix JavaScript code with PHP a lot.
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
<script type="text/javascript"> var _baseUrl = "<?= base_url() ?>"; </script> 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.
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');
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.
|
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")?>' 

2 Comments

how to receive in the function and hoew i would send to the desired place multi.php which is in the libraries folder
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
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).

Comments

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

Comments

0

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

url:"./pages/test.php" 

Comments

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();

Comments

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' } }); 

Comments

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

2 Comments

So mean siteurl+"libraries/multi.php/" is the url
Here url field means a string containing the URL to which the request is sent. In this case it is siteUrl + "libraries/multi.php/". Hope this works.:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.