I'm using cakePHP 3.0 and I have a problem while using ajax. Indeed, I would like to execute the action "viewTab" of my controller "SpecimensController" on an array coming from my view "specimen"
<script type="text/javascript" >
var tab = new Array();
function updateResult(){
$.ajax({
type:"POST",
url:"<?php echo Router::url(array('controller'=>'specimens','action'=>'index'));?>",
data:{datas: $("select[name='filtreVariable\\[\\]']").map(function(){return $(this).val();}).get()},
dataType: 'text',
async:false,
success: function(data){
alert('success');
},
error: function (data) {
alert("error");
}
});
}
$("#filtre").submit(function(){
updateResult();
});
</script>
The action "viewTab" is just doing:
echo "success";
But I can't find the right url so the function success can be called. I tried many things and I always have the function error of ajax that is called. :/
If you haven't yet, place this line of code in your AppController where your initialize function is:
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
In your controller for the function you're calling via ajax, try something like this:
public function index()
{
$data = ['testing'];
$this->set(compact('data'));
$this->set('_serialize', 'data');
}
http://book.cakephp.org/3.0/en/controllers/components/request-handling.html
Related
I am trying to call php function through Ajax in same php file.
User enter voucher_id then call ajax that ajax call php function that function is in same file.
How to call that function.
function of PHP code:-
function voucherExist($voucherNo, $sCID, $type){
global $pncon;
$uRow = $pncon->query("SELECT * FROM transactions WHERE voucher_no = '{$voucherNo}' AND company_ID = '{$sCID}' AND type = '{$type}'");
return $uRow;
}
Ajax code:-
<script type="text/javascript">
$(document).ready(function (){
$('#voucher_no').on('change', function () {
var voucher_no = $('#voucher_no').val();
$.ajax({
url: "bankVoucherAddEdit.php",
type: "post",
data: "voucherNo"
});
});
});
</script>
You can get an idea from the below:
<? php
function yourFunction($data){
return $data;
}
if (isset($_POST['voucherNo'])) {
echo yourFunction($_POST['voucherNo']);
}
?>
<script>
$.ajax({
url: 'bankVoucherAddEdit.php',
type: 'post',
data: { "voucherNo": voucher_no },
success: function(response) { console.log(response); }
});
</script>
The thing is that you cannot directly call a PHP function from JS. You can achieve this by RESTful API. They are quite simpler and Standard.
In your case, Send any post value as a flag from JS to PHP, if that POST value exists then call PHP function
I am trying to call a controller function from an AJAX call. The view loads from the same controller, but I'm not able to call the function.
Here is the controller code:
// This function will get ajax call where pass two variables and a method name doSomeAction
class Posts extends \Core\Controller
{
public function addNewAction()
{
View::renderTemplate('Posts/addnew.html', [
// 'posts' => $posts
]);
}
public function doSomeAction()
{
echo "here";
// your action code ....
}
}
Here is my Ajax script call function
<script>
$(".submitcontr").click(function() {
$.ajax({
url: 'POST/doSomeAction',
dataType: 'json',
data: {
id: 'value'
},
success: function (data) {
console.log(data);
},
error: function () {
alert('error');
}
});
});
</script>
Laravel 5.
I load view from controller using ajax.
When load first time then everything okay.
But when press f5 then it not work. ctrl+f5 then Okay.
I don't know what happen.
Please help me.
Controller
class TopSliderController extends Controller
{
public function index(Request $request)
{
return view('home.top_slider');
}
}
My javascript
(function($) {
load_view("/topic", "GET", "#test_top", []);
}(jQuery));
function load_view(url, method, field_id, param = []) {
$.ajax({
url: url, //+= '?_=' + (new Date()).getTime()
method: method,
data: {param},
timeout: 10000,
datatype: "html",
success:function(result)
{
$(field_id).html(result);
}
});
}
View
<div id="test_top">
</div>
I am trying to retrieve rows from database by making an AJAX call to the controller function. I have no error messages and the code is executing properly upto $("#msgbox").html("Type the name of someone or something...");
I think the controller is not called properly from the view. Below is my MVC code.
My view: welcome_message.php
<div id="centersearch">
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
var start=/^((?!PART).)*$/
var word=/^((?!PART).)*$/
$("#contentbox").live("keyup",function()
{
var content=$(this).text(); //Content Box Data
var go= content.match(start); //Content Matching #
var name= content.match(word); //Content Matching #abc
var dataString = 'searchword='+ name;
//If # available
if(go.length>0)
{
$("#msgbox").slideDown('show');
$("#display").slideUp('show');
$("#msgbox").html("Type the name of someone or something...");
//if #abc avalable
if(name.length>0)
{
$.ajax({
type: "POST",
url: "<?php $this->load->helper('url'); echo base_url();?>index.php/Welcome/ ?>", // Database name search
data: dataString,
cache: false,
success: function(data)
{
$("#msgbox").hide();
$("#display").html(data).show();
}
});
}
}
return false();
});
//Adding result name to content box.
$(".addname").live("click",function()
{
var username=$(this).attr('title');
var old=$("#contentbox").html();
var content=old.replace(word," "); //replacing #abc to (" ") space
$("#contentbox").html(content);
var E="<a class='red' contenteditable='false' href='#' >"+username+"</a>";
$("#contentbox").append(E);
$("#display").hide();
$("#msgbox").hide();
});
});
</script>
My controller: Welcome.php
public function search()
{
$this->load->database();
//load the model
$this->load->model('select');
//load the method of model
$data['s']=$this->select->search();
$this->load->view('welcome_message', $data);
}
My model: Select.php
public function search($datastring)
{
$query = $this->db->select('*')
->from('country')
->where('from', $datastring)
->get();
return $query;
}
You can call your helper in your constructor
function __construct() {
parent::__construct();
$this->load->helper('url');
}
And your ajax url is
url: "<?php echo base_url();?>index.php/Welcome/search?>",
You should include the URL helper in the constructor of the class.
function __construct() {
parent::__construct();
$this->load->helper('url');
}
It looks like you're on CI, so unless you have a route specified your ajax URL is incorrect.
<?php echo base_url();?>index.php/welcome/search ?>
Load the url helper somewhere above in the PHP code itself like in the constructor function of your controller
url: "<?php $this->load->helper('url'); echo base_url();?>index.php/Welcome/ ?>" ==> This line is wrong
So this how your ajax request should be
$.ajax({
type: "POST",
url: "<?php echo base_url();?>index.php/Welcome ?>", // Database name search
data: dataString,
cache: false,
success: function(data)
{
$("#msgbox").hide();
$("#display").html(data).show();
}
});
Load url helper in your controller
$this->load->helper('url');
And use this for your ajax call.
url: "<?php echo site_url('welcome');?>",
Please refer this documentation
http://www.codeigniter.com/userguide3/helpers/url_helper.html#site_url
If you want to call a function of the current controller, you have to create a instance of the current controller in following way:
<?php
$CI =&get_instance();
$CI->method($param);
?>
I am trying to call controller in module using Ajax in prestashop 1.5, and I'm having hard time doing this.
I've created controller in module under the path:
$refresh_url = ($this->_path)."front/blockdiscoversellers.php";
and made instructions for button in js like:
var refresh = {
call: function() {
var $refresh = $("#manufacturer-refresh");
$refresh.click(function(e) {
refresh.ajax();
e.preventDefault();
});
},
ajax: function() {
var url = $("#manufacturer-refresh").data("url");
$.ajax({
url: url,
type: 'get',
data: {
controller : 'BlockDiscoverSellers',
ajax : true
},
dataType: "json",
success: function(data) {
console.log(data);
}
});
}
};
and the body of controller looks like :
class BlockDiscoverSellers {
public function __construct()
{
die(var_dump($this->refreshManufacturers()));
}
public function refreshManufacturers()
{
$result = array("test" => "TESTER");
return Tools::jsonEncode($result);
}
}
I'm getting success in Ajax call but it looks like class and constructor are not initiated, so I am quite stuck with this problem.
It appears that prestashop when you use ajax call uses only structural type of programming. That means in ajax call there can be no class of any sort bootstrap will never initiate it even with controller parameter, and you have to die at the end of file ...