how to call a codeigniter function from jquery ajax - php

I'm using the jquery-datatables-editable plugin at https://code.google.com/p/jquery-datatables-editable/wiki/EditCell#Client-side_configuration with CI.I have:
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('#myDataTable').dataTable().makeEditable({
sUpdateURL: "/js/jquery-databatables-editable/AjaxUpdate_1.php/index"
});
});
</script>
on the client side page. I want to be able to post to a class a CI library class/function on the server side, but the above does not seem to work. Is there a way to do this?

That's depends on the configuration you have.
Mostly common, if you are using the default setup:
In you Javascript file or view (if inline)
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$('#myDataTable').dataTable().makeEditable({
sUpdateURL: "/index.php/your_controller/function_name"
});
});
</script>
In your routes.php
$route["function_name"] = "your_controller/function_name/";
Lastly, you need to create a function in your controller and output the expected format:
function function_name(){
echo json_encode('whatever you need to return');
}
Hope it helps!

Related

geocomplete is not a function using autocomplete

Hi i am using autopopulated code getting the error geocomplete is not a function if is used it in locally (seperated file) it works for me what can be the error
$(function () {
$("#location").geocomplete({
//alert("gine");
details: ".geo-details",
detailsAttribute: "data-geo"
});
});
Pls suggust
You must to add the jquery.min.js and jquery.geocomplete.min.js before call the geocomplete() method:
<script src="/javascripts/jquery.min.js" type="text/javascript"></script>
<script src="/javascripts/jquery.geocomplete.min.js" type="text/javascript"></script>
<script type="text/javascript">
$("#location").geocomplete({
details: ".geo-details",
detailsAttribute: "data-geo"
});
</script>

How to call controller/action in zend framework via jquery/ajax?

How to call controller/action in zend framework via jquery/ajax?
I tried this
<script>
$(function() {
$(".tbl_repeat tbody").tableDnD({
onDrop: function(table, row) {
var orders = $.tableDnD.serialize();
$.post("<?php echo $this->baseUrl();?>/Indexcodelist/indexcodelistsearch/",{order : orders });
}
});
});
</script>
this code isn't calling the controller's action method, how do i achieve it?
Problem occurred that we have to use single or double quotes when giving the name of passing data in $.post(...) or $.ajax(...) otherwise it interpret it as an javascript object not a name.
$.post("<?php echo $this->baseUrl();?>/Indexcodelist/indexcodelistsearch/",{'order':orders});
^-----^
Here is my way to work :
<script type="text/javascript">
$('a.validate').click(function(){
//alert($(this).attr('href'));
$.post('/application/object/validate',{type:'type', id:$(this).attr('href'), pkObject:$('#pkObject').val()},function(data){
return false;
});
</script>
Hope it helps.

New to jQuery - why isn't this simple function working?

I'm building a web application inside CodeIgniter and I've decided to have a fiddle with jQuery and Javascript - something I've never really used before.
I have included jQuery in my header using Google Libraries:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
And now I'm fiddling with the SIMPLEST of jQuery but it's not working.
<p><?php if ($post->love) { echo $post->love; } else { echo 0; } ?></p>
<script type="text/javascript">
$("#lovecounter").click(function() {
alert("Click");
});
</script>
I am clicking on the lovecounter link but there is no alert. If I add simple javascript to the actual anchor tag it works though.
<p><?php if ($post->love) { echo $post->love; } else { echo 0; } ?></p>
Any ideas? What am I doing wrong?
Wrap your code in ready handler:
$(document).ready(function(){
$("#lovecounter").click(function() {
alert("Click");
});
});
Working Example
This works perfectly fine for me:
<p>asdf</p>
<script type="text/javascript">
$(document).ready(function() {
$("#lovecounter").click(function() {
alert("Click");
});
});
</script>
You should include a $(document).ready(function() {}); just like Sarfraz said. Otherwise, it would not find anything with an id of lovecounter because nothing on the page has loaded yet. If you wait for it to load, then it will find the element. Here is some documentation: http://www.w3schools.com/jquery/event_ready.asp. Here is a full jQuery lesson: http://www.codecademy.com/tracks/jquery.

javascript function not working after page load

why my code is not working? I have called a javascript function after page load PHP script.like below:
<script>
setTimeout("fb_login()",100);
</script>
fb_login() function is on same page
function fb_login()
{
alert("ok");
}
Tried with setTimeout("fb_login",100); too. but not working.
I have checked console, but it's giving no error.
Just change it to:
<script>
setTimeout(fb_login, 100);
</script>
Change this code:
<script>
setTimeout("fb_login()",100);
</script>
to this:
<script>
setTimeout(fb_login,100);
</script>
Good explanation from similar post - How can I pass a parameter to a setTimeout() callback?
It might be you given less time in setTimeout but and it's calling your function befor page loaded fully. So Try to increase time.
<script>
setTimeout("fb_login()",1000);
</script>
Make sure that fb_login is being initialized before calling otherwise it will give error. Either use document.ready or add that function before it is called. Does it give you some error like "fb_login is undefined" ?
try this
<script>
window.onload = function(){
setTimeout(fb_login,100);
};
function fb_login(){
alert("ok");
}
</script>
EDIT: first check if the below is working or not, if it does not work then pbm is somewhere else
window.onload = function(){
setTimeout(function(){
fb_login();
},100);
};

Facebook failing on XFBML call using Yii Framework

I have used this same call in other IFRAME Facebook apps, but it gives me nothing at all in terms of output. I am trying it on Yii and getting nothing.
<script type="text/javascript">
window.onload = function() {
FB_RequireFeatures(["XFBML"], function() {
FB.init('xxxxxx', 'xd_receiver.htm');
FB.XFBML.Host.get_areElementsReady().waitUnitlReady(function()
{
document.getElementById("container").style.visibility = "visible";
});
});
};
</script>
<script type="text/javascript">
function publish() {
FB_RequireFeatures(["Connect"], function() {
FB.init('xxxxxx', 'xd_receiver.htm');
FB.ensureInit(function() {
FB.Connect.streamPublish();
});
});
}
</script>
<fb:serverFbml style="width: 755px;">
<script type="text/fbml">
<fb:fbml>
<fb:request-form
action="http://apps.facebook.com/ixxxx"
method="POST"
invite="true"
type="rrrrr"
content="rrrrr <?php echo htmlentities("<fb:req-choice url=\"http://apps.facebook.com/XXXX\" label=\"Authorize My Application\"") ?>" >
<fb:multi-friend-selector showborder="false" actiontext="Invite your friends to use SuperThief.">
</fb:request-form>
</fb:fbml>
</script>
</fb:serverFbml>
It might be helpful to check that you have your new Yii site setup correctly for Facebook. You might not be including the right JavaScript libraries in the layout file's , or you may have forgotten to add the right xmlns attribute to the tag. For a refresher on this stuff head over to the Facebook documentation here:
http://wiki.developers.facebook.com/index.php/Connect/Setting_Up_Your_Site

Categories