post Laravel and VUEJS - php

i´m traying initiate with vueJS and i´m doing a aplication that update a register of my DB but always returned a 405 error in my web browser console. I´m trying send my token in request axios, and i think that is my problem, because with ajax always have to send token in the header petition. this is my actual code vueJS, HTML and route Laravel.
thanks
VUEJS
require('./bootstrap');
window.Vue = require('vue');
Vue.component('usuarios-component', require('./components/usuariosComponent.vue').default);
// inicio de VUE
const app = new Vue({
el: '#contenedorVue',
data:{
id: '',
nombreUsuario: '',
email: '',
password: '',
direccion: '',
token: '',
arrayTasks:[],
},
methods: {
enviar(){
let url = '/actualizarDatos';
axios.post(url, {
id: this.id,
nombreUsuario: this.nombreUsuario,
email: this.email,
password: this.password,
direccion: this.direccion,
token: this.token,
}).then(function(response){
this.arrayTasks = response.data;
}).catch(function(error){
console.log(error);
})
}
}
});
HTML
#extends('layouts.app')
#section('content')
#include('layouts.sidebar')
<!-- SE ESTABLECE V-MODEL para usarlo con vue, es como el ID -->
<div id="contenedorVue">
<div class="d-flex flex-row justify-content-center">
<form method="post">
<input type="hidden" v-model="token" value="{{csrf_token()}}">
<div class="form-row">
<div class="col">
<label for="email">Nombre</label>
<input type="text" class="form-control" v-model="nombreUsuario" value="{{$usuario->nombre}}">
</div>
<div class="col">
<label for="email">Email</label>
<input type="email" class="form-control" v-model="email" aria-describedby="emailHelp" value="{{$usuario->email}}">
</div>
</div>
<div class="form-row">
<div class="col">
<label for="password">Contraseña</label>
<input type="password" class="form-control" v-model="password" value="{{$usuario->password}}">
</div>
</div>
<div class="form-row">
<div class="col">
<label for="direccion">Direccion</label>
<textarea v-model="direccion" class="md-textarea form-control" rows="3">
{{$usuario->direccion}}
</textarea>
<input type="hidden" value="{{$usuario->id}}" v-model="id">
</div>
</div>
<!-- EMPEZAMOS LA FUNCIONALIDAD CON VUE DE ESTA MANERA LE DAMOS FUNCIONALIDAD AL BOTON-->
<button class="btn btn-primary mt-5" #click="enviar">Actualizar</button>
</form>
</div>
</div>
#endsection
CONSOLE ERROR
1:1 POST http://www.bonos.local/datosPersonales/1 405 (Method Not Allowed)
error post laravel
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods: GET, HEAD.
ROUTES
Route::get('/datosPersonales/{id}', 'HomeController#datosPersonales')->name('datosPersonales');
Route::post('/actualizarDatos', 'UsuariosController#actualizarDatosPersonales')->name('actualizarDatosPersonales');
image
image 2

you need to prevent default submit form
<form method="post">
to
<form #submit.prevent="enviar">
<button class="btn btn-primary mt-5" type="submit">Actualizar</button>
</form>
here button need to have type="submit" so once you click that button it will submit form
here post and get will handel in enviar() function
ref link https://v2.vuejs.org/v2/guide/events.html#Event-Modifiers

it´s essential to do
npm run watch
or
npm run dev
for all changes be appliead... sorry for my numb

Related

Using 2 recaptcha on same page and getting different values for both via AJAX

I have 2 Google Recaptcha's on same page in different forms (Login and Register Forms). I had to put both the forms on the same page due to the designing requirements which can't be changed. Now the problem is that the 1st form is the login form followed by the register form in the 2nd. Now, the recaptcha on the login form takes effect for register form as well. I mean that I can see recaptcha on both the forms but the one in register form doesn't work (which means it doesn't sends the correct value to the PHP page where the form will be processed). When I tried verifying the recaptcha on the login form and submitting the register form it worked. This means that the 1st loaded recaptcha (on the login form) is working for both which should not be. Both the recaptchas should work separately for each forms. Also, when I get the values of both the recaptchas via AJAX to send to the PHP page for the server side validation I am using captcha: grecaptcha.getResponse() for both. THIS CAN BE THE ACTUAL PROBLEM I GUESS. If I could get separate values for both maybe the things would have gone down well. Any help would be appreciated.
Here are my codes.
<div class="container">
<div class="row">
<div class="col-md-6">
<h3 class="com-reps cr-panel">Login Now</h3>
</div>
<div class="col-md-6">
<h3 class="com-reps cr-panel">Register Now</h3>
</div>
<div class="col-md-6">
<div class="post-holder">
<div class="login-message"></div>
<div class="post-content">
<form method="post" action="">
<div class="form-group">
<input type="text" id="uname" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<input type="password" id="pass" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="<?php echo $mainf['set_recaptcha_sitekey']; ?>"></div>
</div>
<div class="form-group">
<input type="submit" id="login" value="Log In" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="post-holder">
<div class="register-message"></div>
<div class="post-content">
<form method="post" action="">
<div class="form-group">
<input type="text" id="name" class="form-control" placeholder="Full Name">
</div>
<div class="form-group">
<input type="text" id="username" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<input type="password" id="password" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<input type="password" id="cpass" class="form-control" placeholder="Confirm Password">
</div>
<div class="form-group">
<input type="text" id="email" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<input type="text" id="dob" class="form-control" placeholder="Date of Birth">
</div>
<div class="form-group">
Sex
<input type="radio" id="sex" name="sex"> Male
<input type="radio" id="sex" name="sex"> Female
</div>
<div class="form-group">
<input type="checkbox" id="legal" name="legal"> I accept the Terms of Service and Privacy Policy.
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="<?php echo $mainf['set_recaptcha_sitekey']; ?>"></div>
</div>
<div class="form-group">
<input type="submit" id="submit" value="Register" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#login").click(function(){
var dataString = {
uname: $("#uname").val(),
pass: $("#pass").val(),
captcha: grecaptcha.getResponse()
};
$.ajax({
type: "POST",
//dataType : "json",
url: "processes/login.php",
data: dataString,
cache: true,
beforeSend: function(){
$("#login").val("Please wait...");
$(".login-message").hide();
},
success: function(html){
$('.login-message').html(html).fadeIn();
if($('.login-message').find('#responseBox').hasClass('alert-success')){
setTimeout(function(){
window.location.replace("index.php");
}, 500);
}else{
$("#login").val("Log In");
}
}
});
return false;
});
$("#submit").click(function(){
var dataString = {
name: $("#name").val(),
uname: $("#username").val(),
pass: $("#password").val(),
cpass: $("#cpass").val(),
email: $("#email").val(),
dob: $("#dob").val(),
sex: $("input[name='sex']:checked").val(),
captcha: grecaptcha.getResponse()
};
$.ajax({
type: "POST",
//dataType : "json",
url: "processes/register.php",
data: dataString,
cache: true,
beforeSend: function(){
$("#submit").val("Please wait...");
$(".register-message").hide();
},
success: function(html){
$('.register-message').html(html).fadeIn();
$("#submit").val("Register");
}
});
return false;
});
});
</script>
I got the solution. In case someone comes looking for this it will help them.
In both HTML Forms replace
<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
to
<div id="recaptcha1"></div> and <div id="recaptcha2"></div> respectively.
Then replace your script calling from
<script src="https://www.google.com/recaptcha/api.js></script>
to
<script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
And insert these code in your <script></script>. Note that this should be outside of $(document).ready(function() { and above your next form submission AJAX codes.
var recaptcha1;
var recaptcha2;
var myCallBack = function() {
recaptcha1 = grecaptcha.render('recaptcha1', {
'sitekey' : 'YOUR_SITE_KEY'
});
recaptcha2 = grecaptcha.render('recaptcha2', {
'sitekey' : 'YOUR_SITE_KEY'
});
};
Next and last thing, change the captcha: grecaptcha.getResponse() in your form submission AJAX code to captcha: grecaptcha.getResponse(recaptcha1) and captcha: grecaptcha.getResponse(recaptcha2) respectively (for both AJAX codes of login and register forms in my case). For full form submission code please refer to the code in my question above. Just replace the captcha: part here as said.
Done! Now you can proceed to server side validation in your language (PHP or ASP whatever).

Dropzone - send image to controller with other form data on submit

I'm working in laravel 5.3 project.
before asking question I have tried these links :
1st , 2nd , 3rd , 4th ,5th but no luck.
let me show you my code.
details.blade.php
<form class="form" method="post" action="{{url('add_user_photo_gallery')}}" id="my-awesome-dropzone" name="validate_resume">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
<div class="card-body">
<div class="row">
<div class="col-md-12 col-sm-12 text-center">
<div class="form-group floating-label">
<div id="dropzonephoto" class="dropzone dz-clickable form-fileupload">
<div class="dz-message dz-default" style="text-align:center;">
<span>Click Here to upload Photo </span>
</div>
</div>
</div>
<small>jpg, png , jpeg - upto 1 Mb max</small>
</div>
<div class="col-md-12">
<input type="text" name="first_name" />
</div>
<div class="col-md-12">
<input type="text" name="last_name" />
</div>
<div class="col-md-12">
<input type="email" name="email" />
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" id="btn_save_photo" class="custom-btn primary-btn pull-right" required>
<input type="reset" id="btn_reset_photo" class="custom-btn warning-btn pull-right" value="Cancle" required>
</div>
</div>
</div>
</div>
</form>
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone('#dropzonephoto', { // Make the whole body a dropzone
url: base_path + '/add_user_photo_gallery',
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
paramName: "images",
parallelUploads: 1,
maxFilesize: 1.0,
maxFiles:20,
acceptedFiles: ".png,.jpg,.jpeg",
dictInvalidFileType: 'This file type is not supported.',
dictFileTooBig:'File size too Big',
addRemoveLinks: true
});
public function add_user_photo_gallery(Request $request){
dd($request->all());
}
In my controller function I print request that coming from form. I'm getting all the field except dropzone image.
using above dropzone script when I select image it's immediately go to controller and I can see all image details in console / network.I don't want that so I added below code in my script.
autoProcessQueue:false,
$('#btn_save_photo').on('click',function(e){
e.preventDefault();
myDropzone.processQueue();
});
After add this code image is upload on submit button but other form element is not submitted. and this upload process is shown in inspect element / network section .
So what I want is on submit button want to page refresh and print all form data with dropzone image.
Anyone is here to solve my problem ?

How to submit form on one page to a different page using ajax call in jquery

I have a page called page2.php. It has a form that allows you to search via jquery ajax call. The code is below. My question is, I have a different form on page1.php. How can I submit the form on page1.php to go to page2.php and have it execute the page2.php code below using the data from the form on page1.php? I know this is most likely simple, having a brain fart right now.
$(function() {
$.validate({
form: '#form_search',
validateOnBlur: false,
errorMessagePosition: 'top',
onSuccess: function(form) {
var formval = $(form).serialize();
var formurl = '/page2.php?a=search';
$('#form_results').html('<div class="form_wait_search">Searching, please wait...<br><img src="/images/search-loader.gif"></div>');
$.ajax({
type: 'POST',
url: formurl,
data: formval,
success: function(data){
var json = $.parseJSON(data);
$('#form_errors').html(json.message);
$('#form_results').html(json.results);
}
});
return false;
}
});
});
UPDATE
Here is the forms Im referring to.
On page1.php this is like a module on the right side bar. Just a form that I want to post to page2.php
<div class="scontent_box1">
<strong class="box_title"><i class="fa fa-search fa-flip-horizontal"></i> Find Locations</strong>
<form method="post" action="/page2.php">
<div class="form-group">
<label>Street Address</label>
<input type="text" class="form-control" name="ad" placeholder="Enter Street Address...">
</div>
<div class="form-group">
<label>City, State or Zip Code</label>
<input type="text" class="form-control" name="ct" placeholder="Enter City, State or Zip Code...">
</div>
<button type="submit" class="btn btn-default blue_btn">Search</button>
</form>
</div>
Now here is the form on page2.php that executes the ajax code above. I want page1.php to submit to page2.php and envoke the same jquery code above.
<div class="row no_gutters">
<div class="search_page">
<div id="form_errors"></div>
<form method="post" id="form_search">
<div class="form-group">
<label for="ad">Street Address<span class="reqfld">*</span></label>
<input type="text" class="form-control" data-validation="required" id="ad" name="ad" placeholder="Enter Street Address..." value="">
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="ct">City, State or Zip Code<span class="reqfld">*</span></label>
<input type="text" class="form-control input-sm" data-validation="required" id="ct" name="ct" placeholder="Enter City Name..." value="">
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-xs-12">
<button type="submit" class="btn btn-default blue_btn btn-block">Search Locations</button>
</div>
<div class="col-md-8"></div>
</div>
</form>
</div>
<div id="form_results"></div>
</div>

laravel 5.1 form and routing issue

Hi i am trying to get the output from my form to save and even when i try to echo(print_r) the output it but it simply goes to the post request and does not echo any output.
my route looks like:
Route::controller('stove', 'StoveController', [
'anyData' => 'stove.data',
'getIndex' => 'stove',
]);
Route::get('newstove', 'StoveController#addData');
Route::post('newstove', 'StoveController#store');
my controller:
public function addData()
{
return view('stoves.new');
}
public function store()
{
$input = Request::all();
Stove::create($input);
return redirect('stove');
}
and finally my form is
<form class="form-horizontal" action="/stove">
<fieldset>
<div class="control-group">
<label class="control-label" for="stoveno">Stove Number</label>
<div class="controls">
<input type="text" class="span4" id="stoveno" value="CP001000">
</div> <!-- /controls -->
</div> <!-- /control-group -->
<div class="control-group">
<label class="control-label" for="refno">Ref Number</label>
<div class="controls">
<input type="text" class="span4" id="refno" value="cff001">
</div> <!-- /controls -->
</div> <!-- /control-group -->
<div class="control-group">
<label class="control-label" for="manufacturedate">Manufacture Date</label>
<div class="controls">
<input type="date" class="span4" id="manufacturedate">
</div> <!-- /controls -->
</div> <!-- /control-group-->
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<button class="btn">Cancel</button>
</div> <!-- /form-actions -->
</fieldset>
</form>
Thanks
Change the first line of your form to the following..
<form class="form-horizontal" action="/newstove" method="post">
This should submit your form via the POST method to the last route in your routes file.
From what I can see the first part of your routes file is not required...
Route::controller('stove', 'StoveController', [
'anyData' => 'stove.data',
'getIndex' => 'stove',
]);
Route::post('newstove', 'StoveController#store');
You need to add method="post" to the form. and change the action="/stove" to action="/newstove"
Also, isn't it Route::resource for adding controllers to the route list?

laravel not loading my form section?

im trying to build a user login and registration form and this is my route :
Route::get('/register', function()
{
return View::make('register');
});
Route::get('/register', function()
{
$user = new User;
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
$username = Input::get('username');
return View::make('registered')->with('username',$username);
});
and this is my html :
<div class="container">
{{ Form::open(array('url' => 'register', 'class' => 'form-horizontal')) }}
<fieldset>
<!-- Form Name -->
<legend>Form Name</legend>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="username"></label>
<div class="col-md-4">
<input id="username" name="username" type="text" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Password input-->
<div class="form-group">
<label class="col-md-4 control-label" for="password"></label>
<div class="col-md-4">
<input id="password" name="password" type="password" placeholder="" class="form-control input-md" required="">
</div>
</div>
<!-- Appended checkbox -->
<div class="form-group">
<label class="col-md-4 control-label" for="appendedcheckbox"> </label>
<div class="col-md-4">
<div class="input-group">
<input id="appendedcheckbox" name="appendedcheckbox" class="form-control" type="text" placeholder="">
<span class="input-group-addon">
<input type="checkbox">
</span>
</div>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"> </label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-inverse"> </button>
</div>
</div>
</fieldset>
</div>
few problems :
1.
my form does not loads and i see just
the last button for submitting the form and : ' you have registered in $username ' which i design to loads AFTER user submitted
2.my localhost:8000 loaded laravel first page one time but when i began to work on the project i just receiving blank white page and currently accessing my file like this : http://localhost/vendor/bin/crm/public/register
3.
is hashing in laravel secure enough? or should i do something else ?
4.
my way of doing this is alright or there is a better way for login and reg using laravel ?
You have two routes responding to get requests on /register. Change the second one to Route::post(...) and I would also change both to just register. There isn't a need to prepend a slash onto your routes.
Hashing in Laravel is secure and shouldn't be something you have to worry about.
There really isn't a "right" way of doing things, it really depends on how the rest of your app works, how complicated it is, and how easy it should be to maintain. If it were me though, I would have a LoginController with a method for showing the view and a method for creating the user and have those methods respond to the request rather than putting everything right in your routes.php file.
You are also missing a {{ Form::close() }} at the end of your view as well.

Categories