ajaxfosuserbundle registration symfony 2.8 - php

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

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.

Formatting form data using JQuery for AJAX POST and access easily in Laravel (or PHP)?

I need to send form data over AJAX so I can access it easily in Laravel (or using standard PHP methods). It's actually a form using Stripe Checkout that I'm stopping from being auto submitted so I can send over AJAX. Currently I have the code below but suspect variable "formdata" is not formatted correctly before being referenced in $.ajax.
What do I need to do to it to make it formatted correctly?
Ideally I want to access the separate form elements on the backend as regular POST vars (or actually I'm using Laravel, so via Request->input() method), will this do that?
Thanks
$('#booking-form').get(0).submit = function() {
var formdata = $(this).serializeArray();
$.ajax({
type: 'POST',
url: 'book',
data: { formdata },
success: function (data)
{
// Form submitted and processed correctly, success returned
},
error: function (data)
{
// console.log('Error:', data.responseText);
}
});
Backend (Laravel 5.4, but I could use raw PHP if I had to):
public function book(Request $request)
{
$formfield1 = $request->input('formfield1');
$formfield2 = $request->input('formfield2');
[etc.]
}
Try this:
$(document).ready( function() {
$('#booking-form').submit( function( e ) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'book',
data: $('#booking-form').serialize(),
success: function (data) {
// Form submitted and processed correctly, success returned
},
error: function (data) {
// console.log('Error:', data.responseText);
}
});
});
});
Not really familiar with Laravel, but certainly accessing the form fields via native PHP (i.e. $_POST['formfield1']) should work perfectly.

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.

prestashop ajax controller call

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

Custom Form Validation and Submit

I have a form validation run when the submit button is selected but i do not want the default submit function to run. Instead i want to run an ajax post code to store the data in the form along with post the data from server side.
I am having trouble finding where to cancel the default submit and add in the ajax code.
Here is the validate code i have
$("#formEnroll").validate({
errorElement:"p",
submitHandler: function(form) {
var phoneNumber = form.day_phone.value;
var phoneParts = phoneNumber.split("-");
form.dayArea.value = phoneParts[0];
form.dayPrefix.value = phoneParts[1];
form.daySuffix.value = phoneParts[2];
form.submit();
}
});
And here is the ajax code i want to run instead of the default submission
$.ajax({
type: "POST",
url: "/php/localProxy.php",
data: $('#formEnroll').serialize(),
success: function (response) {
document.location = 'http://www.xxxx.com/thank-you';
},
error: function () {
alert('There was a problem!'); // handle error
}
});
you can call the the ajax function instead of form.submit();
form.submit(); will submit your code using default submission.
$('#formEnroll').on('submit', function(e){
e.preventDefault();
$(this).validate({
errorElement:"p",
submitHandler: function(form) {
var phoneNumber = form.day_phone.value;
var phoneParts = phoneNumber.split("-");
form.dayArea.value = phoneParts[0];
form.dayPrefix.value = phoneParts[1];
form.daySuffix.value = phoneParts[2];
submitform();
}
});
})
function submitform(){
$.ajax({
type: "POST",
url: "/php/localProxy.php",
data: $('#formEnroll').serialize(),
success: function (response) {
document.location = 'http://www.xxxx.com/thank-you';
},
error: function () {
alert('There was a problem!'); // handle error
}
});
}
You could wrap your validate plugin into a generic form submit function to stop standard execution.
$('#formEnroll').on('submit', function(e)
{
e.preventDefault(); // stop form submission
// Run validation
$(this).validate({
errorElement:"p",
submitHandler: function(form) {
var phoneNumber = form.day_phone.value;
var phoneParts = phoneNumber.split("-");
form.dayArea.value = phoneParts[0];
form.dayPrefix.value = phoneParts[1];
form.daySuffix.value = phoneParts[2];
submitform(); // call your ajax function here
}
});
});
function submitform(){
$.ajax({
type: "POST",
url: "/php/localProxy.php",
data: $('#formEnroll').serialize(),
success: function (response) {
document.location = 'http://www.xxxx.com/thank-you';
},
error: function () {
alert('There was a problem!'); // handle error
}
});
Instead of using the event object e you can also just return false; at the end of the .on function to stop default events from firing.
You can send the data by ajax using the click event in the jquery by clicking the submit button and that function should return false for avoiding the default submission by the way you can send the data using ajax and avoid the default submission.

Categories