Welcome Guest, Not a member yet? Register   Sign In
Ajax post is not working
#1

[eluser]bsrbvr[/eluser]
Hi,
I am using codeigniter 2.1.0 and php 5.3.5.
I am trying to get the data entered in textbox after submitting using Ajax. But I am not able to get any alert message.

My view page:

<input type="text" id="txtLocation" name="txtLocation" class="tb1"></input>
<input type="image" name="myclicker" src="<?base_url()?>images/search_button.png" id="offersearch" />
[removed]
$(document).ready(function(){
$('#offersearch').click(function () {
var msg = $("#txtLocation").val();

$.post("/location_ctrl/getLatLong", { 'item': msg },
function(data){
alert(data.result);
}, "json");

});
});

[removed]

location_ctrl.php page:

class Location_ctrl extends CI_Controller{

public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('offerModel');
}
public function getLatLong()
{
$place = trim($this->input->post('item'));
$array = array('result' => $place);
header('Content-Type: application/json',true);
echo json_encode($array);


}
}

Where is the problem coming here.

Thanks.
#2

[eluser]InsiteFX[/eluser]
Try this:
Code:
<head>
// replace $ in $cript with s
<$cript type="text/javascript" charset="utf-8">
    //&lt;![CDATA[
        var base_url = "&lt;?php echo base_url(); ?&gt;";
    // ]]>
</$cript>

&lt;/head&gt;

$.post(base_url + "location_ctrl/getLatLong", { 'item': msg },
#3

[eluser]bsrbvr[/eluser]
Hi, I tried with this but I am not getting result.
Code:
[removed]
$(document).ready(function(){
$('#offersearch').click(function () {
      var msg = $("#txtLocation").val();

  $.post(&lt;?=base_url ?&gt; +"location_ctrl/getLatLong", { 'item': msg },
   function(data){
  alert(data.result);
  }, "json");
    
});
});

[removed]

Thanks.
#4

[eluser]InsiteFX[/eluser]
Look how I showed you how to do above thats not the way I showed you!
Code:
$.post(base_url + "location_ctrl/getLatLong", { 'item': msg },
#5

[eluser]eakroko[/eluser]
Code:
$.post(&lt;?=base_url()?&gt; +"location_ctrl/getLatLong", { 'item': msg },

it is base_url()
#6

[eluser]InsiteFX[/eluser]
key it in the way I showed you above! Do you see any php tags there?
Code:
&lt;head&gt;
// replace $ in $cript with s
<$cript type="text/javascript" charset="utf-8">
    //&lt;![CDATA[
        var base_url = "&lt;?php echo base_url(); ?&gt;";
    // ]]>
</$cript>

&lt;/head&gt;

$.post(base_url + "location_ctrl/getLatLong", { 'item': msg },
#7

[eluser]aquary[/eluser]
InsiteFX meant putting double quote around the base_url() path, else you will have javascript error. With YOUR code, it could be like this:

Code:
$.post("&lt;?=base_url()?&gt;" +"location_ctrl/getLatLong", { 'item': msg },

But InsiteFX version is better when you want to use the base_url somewhere else in the javascript

Also, you could try checking the script, with your favourite debugger, firebug, chrome console, etc., to see if you got any errors. If there is no error, have a look at what got posted, and what is the result.
#8

[eluser]Unknown[/eluser]
Using absolute URI is not necessary when the RewriteBase correctly set in the main .htaccess file.


If the codeigniter is deployed on the website root folder, then
Code:
RewriteBase /

Just use the relative URI in the Ajax calls.



If the codeigniter is deployed on the sub folder, then
Code:
RewriteBase /subfolder/

Prefix the /subfolder/<URI> as the relative URI in the Ajax calls.


I already struggled a bit and found this fix after spent some time with different options.

Good Luck.




Theme © iAndrew 2016 - Forum software by © MyBB