prestashop ajax controller call - php

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 ...

Related

There's possible to pass PHP Post using Jquery to a Constructor Class In a Simple MVC Structure?

Here is my JS code I'm trying to pass the data like a post to the class, I want to keep the integrity of the class. I want to get the form data, and via Jquery Ajax pass it as a post, and by different action it receives data values to be passed to the model and rendered by the view.
//Pass a POST to the PHP controller class via Ajax Jquery.
(I know my $.ajax is empty, this is just a example...)
$.ajax({
url: 'php/Sys/Controllers/ControllerUsuario.php',
type: 'POST',
data: { },
error: function() {
},
success: function(data) {
}
});
});
Here is my PHP Class
class ControllerUsuario
{
private $modelUsuario;
private $action;
/*public function actionUsuario( ){
//$codigo = $_POST[codigo];
$this->modelUsuario = new ModelUsuario();
$this->modelUsuario->setCodigo($codigo);
return (new ViewUsuario($this->modelUsuario->selecionarUsuario()))->renderUsuario();
}*/
public function __construct($action) {
$this->action = $action;
switch ($this->action) {
case 'actionUsuario':
//$codigo = $_POST["codigo"];
$this->modelUsuario = new ModelUsuario();
$this->modelUsuario->setCodigo(1);
return (new ViewUsuario($this->modelUsuario->selecionarUsuario()))->renderUsuario();
break;
default:
# code...
break;
}
}
}
Someone can help-me? please?
Please clarify your question
as i understand from your question that you need to send data from ajax to you controller
You need to create object of your data
Ex. Ajax call
$.ajax({
url: 'php/Sys/Controllers/ControllerUsuario.php',
type: 'POST',
data: {action: "actionUsuario", codigo: "Test"},
error: function() {
},
success: function(data) {
}
});
});
and add this to end of your your controller
if(isset($_POST['action']) && $_POST['action']){
return new ControllerUsuario($_POST['action']);
}
I made a Handler to get all data and after that I pass the values to the class, because my problem was with the $_POST inside the constructor class. It was giving me a problem. Error. I solved this. Thank you guys for Helping.

pass the data-id attribute value to a controller method using ajax in codeigniter

I have a button element which has a data-id attribute.I need to to send the data-id to a controller method in codeigniter using jquery ajax post method.I don't have any form .But the ajax request i made can not enter into the controller method .What might be the reason and how i can solve this issue?in my config file i set the base url and rewrite the index.php using htaccess file :
I do get alert('success') after clicking the button
$config['base_url'] = 'http://localhost/mycodeigniter/ci/'
admin controller:
class Admin extends CI_Controller{
public function processReq(){
$this->load->view('admin/processReq');
}
}
button( no form element) :
<button type="button" data-id='approved' class="approved buttons">Approve</button>
ajax method:
$(".buttons").click(function(event){
var status=$(this).data('id');
$.ajax({
url:"<?php echo base_url('admin/processReq') ;?>",
type: "POST",
data: {
status: status
},
success: function(data) {
alert('success');
},
error: function(xhr, status, error) {
var error=xhr.responseText;
alert(error);
}
});
});
You get the alert because the controller sent the browser a reply with a server code of 200. That header response code tells AJAX to call the success function. $this->load->view('admin/processReq'); is where the OK (code 200) came from.
This controller shows another way to handle this so that you can react based on the reply from processReq().
class Admin extends CI_Controller
{
public function index()
{
$this->load->view('admin/processReq'); //if this really is the view file
}
public function processReq()
{
$status = this->input->post('status');
//I am assuming that data('id') is '1' or '0' since you
//don't describe what it actually is I had to make something up.
if($status === '1'){
echo json_encode(array('results' => 'success')) ;
}else{
echo json_encode(array('results' => 'fail')) ;
}
}
}
You need to add one ajax option - dataType: 'json' - for this to work reliably
$.ajax({
url:"<?php echo base_url('admin/processReq') ;?>",
type: "POST",
dataType: 'json',
data: {
status: status
},
Your ajax success function could do this
success: function(data) {
if(data.results === 'success'){
alert('success');
} else {
alert('success');
}
},
An alternative way to indicate a failure would be to output a header with a server code in the 400 or 500 (error) range. If you do that then the ajax error function will be called. SO has lots of examples of how to accomplish that.

ajaxfosuserbundle registration symfony 2.8

I'm using AjaxFOSuserBundle
and trying to make registration via ajax, but getting an error. What does it mean?
Catchable Fatal Error: Argument 1 passed to FOS\UserBundle\EventListener\AuthenticationListener::authenticate() must be an instance of FOS\UserBundle\Event\FilterUserResponseEvent, instance of Tecnocreaciones\Bundle\AjaxFOSUserBundle\Event\FilterUserResponseEvent given
$(function ()
{
$('form#fos_user_registration_form').submit(function (e) {
e.preventDefault();
var $this = $(e.currentTarget);
inputs = {};
// Send all form's inputs
$.each($this.find('input'), function (i, item) {
var $item = $(item);
inputs[$item.attr('name')] = $item.val();
});
// Send form into ajax
$.ajax({
url: $this.attr('action'),
type: 'POST',
dataType: 'json',
data: inputs,
success: function (data) {
if (data.has_error) {
// Insert your error process
alert('WRONG');
}
else {
// Insert your success process
alert('check your email!');
}
}
});
});
});
It seems to be a problem with the AjaxFOSUserBundle code.
The FilterUserResponseEvent from AjaxFOSUserBundle should extends FOSUserBundle class.
You should post your issue on the github project.
The other solution could be to overwrite the registration controller of FosUserBundle (ajaxfosuserbundle extends the fosuserbundle) to fix the event:
http://symfony.com/doc/current/bundles/FOSUserBundle/overriding_controllers.html
Or by redefining the register route.
https://github.com/Tecnocreaciones/AjaxFOSUserBundle/blob/master/Controller/RegistrationController.php#L123

FUELPHP Ajax request without jquery

I have looked everywhere, I found, that FUELPHP not handle Ajax requests, native and easily, as does RubyOnRails for example.
There must be done manually through jquery, unless I'm missing something I see is this: you have to use preventDefault () for the submit event of the form to create a post, product or whatever, and use the $ function post () to send the relevant parameters, which I think is ridiculous for a framework that claims to be based on the best ideas from other frameworks.
Please tell me if I'm wrong, I like FUELPHP, and I'm thinking about choosing it as PHP framework, but I want to be clear about this.
why not you can handle ajax in fuelphp like this.
.01. create ajax request in your view or public/assets/ create javascript file,
<script> $('.login').on('click',function(){
$.ajax({
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (e) {
alert("loading");
}, false);
return xhr;
},
url: "<?php echo \Uri::create('auth/login'); ?>",
type: "POST",
dataType: 'json'
data: {'username':$('#username').val(), 'password':$('#password').val()},
success: function (data) {
alert(data.status);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error");
}
});
});
</script>
.02. after that you can handle post data in classes/controller/ create auth.php and login method like this,
<?php
class Controller_Auth extends \Controller_Template {
function action_login() {
if (\Input::is_ajax()) {
if (\Input::param('username') and \Input::param('password')) {
$username = \Input::param('username');
$password = md5(\Input::param('password'));
//check password and username or anything
$msg = 'fail';
return json_encode(array('status' => $msg));
}
}
...
}
}
?>
you can handle data like this. i think you got something. and this is helpful.

CakePHP - How to pass url with ajax?

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

Categories