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.
Related
I have started learning jquery AJAX. I have run into a problem, and was wondering if you guys could help me. I am trying to pass a php variable back to jquery, but it displays as [object Object]. I will be posting my code below.
index.html:
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function() {
$("p").text($.get("return.php"));
});
});
</script>
</head>
<body>
<p>This is a test!</p>
<button>Click Here</button>
</body>
</html>
return.php:
<?php
$message = "Another test!";
echo $message;
?>
So what is it that I need to do to pass php variable $message into the paragraph using jquery ajax?
I know I could simply do if I changed index.html to index.php, but then if $message later changes, I have to reload the page. I am trying to learn how to make dynamic content without having to reload the page.
Thanks ahead of time for any help you provide! :-)
You'll have to wait until the data is returned before you can use it:
$(document).ready(function(){
$("button").click(function() {
$.get("return.php", function(data) {
$("p").text(data);
});
});
});
Add a callback to get.
$.get("return.php", function(data) {
$("p").text(data);
});
You can use callback function in .get function.
$(document).ready(function(){
$("button").click(function() {
$.get("return.php",function(data){
$("p").text(data);
});
});
});
Here you can pass the datatype as well in which form you want the response from server.
Suppose you want to return anyother datatype(i.e. json)from server, just use datatype with it like this :
$(document).ready(function(){
$("button").click(function() {
$.get("return.php",function(data){
$("p").text(data);
},"json");
});
});
For more detail,refer : http://api.jquery.com/jQuery.get/
I am writing a script using AJAX in JQuery that takes an action_id from exp_actions as the destination URL for the method I need to execute in my mcp file. The reference is made statically. Can the ACT value be found by making a PHP call in the view in a way similar to:
$aid =$this->EE->cp->fetch_action_id('Class_name', 'method_name'); ?
My page currently looks this way:
<script type= "text/javascript">
$(document).ready(function() {
$('form').change(function(e){
var data = $(this).serializeArray();
console.log(data);
$.post(
<? echo'http://ourwebsite.com/ee/admin.php?ACT=44&id=4 ,';?>
data,
function() {
console.log(this);
}
)
});
});
Thanks in advance!
So I found the answer to my question on the expressionengine forums. For those curious, the link is below. Thanks for the help :)
http://expressionengine.com/forums/viewthread/171401/#986831
Assuming you are using that script in a template or a snippet, you could make a call to your addon instead, returning the needed URL:
<script type= "text/javascript">
$(document).ready(function() {
$('form').change(function(e){
var data = $(this).serializeArray();
console.log(data);
$.post(
'{exp:your_addon:method_for_getting_action_id_url}',
data,
function() {
console.log(this);
}
)
});
});
If you want to make an ajax request in the CP, you just need to request the cp url:
/system/index.php?S=0&D=cp&C=addons_modules&M=show_module_cp&module=my_module&method=my_method
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);
};
I just started learning using AJAX with Codeigniter. On my view, I have a textarea and a button which uses AJAX to submit the text in the textarea to my controller, which retrieves data from the database and returns this data to the view. However I am getting the error "disallowed key characters" in the callback function. This happens even when I simply echo a string. What is happening?
Btw, should I use return $result or echo $result in the controller to pass the data back to the webpage?
AJAX
$(function() {
$("#search_button").click(function(e){
e.preventDefault();
var search_location = $("#search_location").val();
$.get('index.php/main/get_places', search_location, function(data){
$("#result").html(data);
console.log(data);
});
});
});
Controller
function get_places($address) {
$search_latlng = $this->geocode_address($address);
$this->load->model('main_model.php');
$result = $this->main_model->get_places($search_latlng);
echo "test";
}
CodeIgniter has restricted the characters in the url to:
$config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-'; //Inside config.php
Chances are you are putting in characters that not in this list in the address when you send the AJAX request. My suggestion would be change the $.get to $.post and then get the post data out in the controller. Something like this:
AJAX
$(function() {
$("#search_button").click(function(e){
e.preventDefault();
var search_location = $("#search_location").val();
$.post('index.php/main/get_places', {'search_location': search_location}, function(data){
$("#result").html(data);
console.log(data);
});
});
});
Controller
function get_places() {
$address = $this->input->post('search_location');
$search_latlng = $this->geocode_address($address);
$this->load->model('main_model.php');
$result = $this->main_model->get_places($search_latlng);
echo $result;
}
As for the echo vs return, use echo.
You're probably not allowing querystrings in your URL and the Ajax function adds querystring to the url. Take a look at the following url to learn how to turn querystrings on:
http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html
i got this script, which i found in the web:
<script type="text/javascript">
$(document).ready(
function () {
$('a.closeEl').bind('click', toggleContent);
$('div.groupWrapper').Sortable(
{
accept: 'groupItem',
helperclass: 'sortHelper',
activeclass : 'sortableactive',
hoverclass : 'sortablehover',
handle: 'div.itemHeader',
tolerance: 'pointer',
onChange : function(ser)
{
},
onStart : function()
{
$.iAutoscroller.start(this, document.getElementsByTagName('body'));
},
onStop : function()
{
$.iAutoscroller.stop();
}
}
);
}
);
var toggleContent = function(e)
{
var targetContent = $('div.itemContent', this.parentNode.parentNode);
if (targetContent.css('display') == 'none') {
targetContent.slideDown(300);
$(this).html('[-]');
} else {
targetContent.slideUp(300);
$(this).html('[+]');
}
return false;
};
function serialize(s)
{
serial = $.SortSerialize(s);
alert(serial.hash);
};
</script>
<div class="serializer">
<a href="#" onClick="serialize(); return false;" >serialize all lists</a>
</div>
<script language="JavaScript" type="text/javascript">var client_id = 1;</script>
at the bottom of the script is the "function serialize", which is called by clicking on the link below. Can someone tell me, how can i send the variable "serial.hash" to a php-file to save it in mysql-database?
many thanks, maschek
Yes as Yacoby says $.post() or jQuery.post
http://docs.jquery.com/Ajax/jQuery.post#urldatacallbacktype
Use Ajax to send request to a PHP script which submits the data to a database. The JQuery documents contain a lot of examples. If you use a HTTP GET request, be warned there is a limit on the amount of data you can pass. This is browser dependant. I would recommend using jQuery.post as the method of sending data.
All the PHP script has to do is read the variables either from $_GET or $_POST (whichever method you use), sanitize them and submit them to the database.
Actually, it seems very simple now:
$.post("test.php",{'func':'serial'},function(data){
alert(data);
});