How to submit vue-google-maps placeInput to server? - php

I have a form in my application. I also have a view component that creates an input field using vue-google-maps.
I use this component within my but there is no name attribute so my server does not recognize a submitted value.
How can I submit the data from the input field to my server? I am using Laravel 5.3.
<template>
<place-input
:place.sync="placeInput.place"
:types.sync="placeInput.types"
:component-restrictions.sync="placeInput.restrictions"
class='form-control'
label='Location: '
name='location'
></place-input>
<pre>{{ placeInput.place | json }}</pre>
</template>
<script>
import { PlaceInput, Map } from 'vue-google-maps'
export default {
data() {
return {
placeInput: {
place: {
name: ''
},
types: [],
restrictions: {'country': 'usa'}
}
}
},
components: {
PlaceInput
},
ready() {
}
}
</script>
<style>
label { display: block; }
</style>
My Laravel form looks like this:
{!! Form::open(['action' => 'CandidateController#store']) !!}
<div class='form-group'>
{!! Form::label('email', 'Email:') !!}
{!! Form::email('email', null, ['class' => 'form-control']) !!}
</div>
<div class='form-group'>
{!! Form::label('phone', 'Phone:') !!}
{!! Form::text('phone', null, ['class' => 'form-control']) !!}
</div>
<div class='form-group'>
<location-input></location-input>
</div>
{!! Form::close() !!}
Where location-input is the vue-google-maps component that generates an input.
When I submit the form and dump the data to the screen no location data is available!
on server
$input = Request::all();
dd($input);
The raw input looks like this (the name attribute on the component does not add to the input field):
How do I submit the location-input data along with my form?

I'd still like to see your generated html but I think the problem is that there's no name attribute on the input you're creating. Thus, it's not part of the posted data.
<place-input
:place.sync="placeInput.place"
:types.sync="placeInput.types"
:component-restrictions.sync="placeInput.restrictions"
class='form-control'
name='location'
label='Location: '
></place-input>

I am not familiar with vue but what about sending your form through vue and adding manually adding the location to the ajax request? Something like
<form v-on:submit.prevent="onSubmit">
//Your form
</form>
and
methods: {
onSubmit: function (event) {
//You can ajax your form data + manually add your location-input from your component
}
}
Would that solve your problem?

Related

Check if Form method is post or patch in Laravel

I have an edit view and i am using a partial _form view.
Is there a way to check if the form is a patch or post?
What i plan to do is to change the hidden field in edit form
#if (form is post)
{!! Form::hidden('signature') !!}
#else
<div class="form-group">
{!! Form::label('signature', 'Signature: ', ['class' => 'col-md-4 control-label']) !!}
<div class="col-md-6">
{!! Form::text('signature', null, ['class' => 'col-md-2 form-control', 'required']) !!}
</div>
</div>
#endif
because this variable is already saved to DB and i want to load it for edit.
Or to check if form is post, that would work also!
I usually pass the variable to a view where I set action, like:
$action = 'store';
Then I use this variable to build route name:
{!! Form::open(['route' => 'post'.$action, ....
And detect what type of action is needed:
#if ($action == 'store')
I guess it's the most readable and simple way to achieve what you're trying to achieve. You can do something similar.
Try this:
$isPut= Request::isMethod('put');
if($isPut) {
//
}

Laravel 5 forms: generate fieldv value instead of having user fill in

I am creating a form that has a field that is required (trip_route_name). I want to generate a name for this (say a random number for example) when the form is submited rather than having the user fill out the form. How do I do that? Here is the code for the form:
<div class="form-group">
<div class="col-md-6">
{!! Form::label('trip_route_name', 'Trip Route Name') !!}
{!! Form::text( 'trip_route_name', null, ['class' => 'form-control']) !!}
<p id="msg_trip_route_name" class="text-danger">{!! $errors->first('trip_route_name', ':message') !!}</p>
</div>
</div>
You can use str_random() helper:
{!! Form::text( 'trip_route_name', str_random(20), ['class' => 'form-control']) !!}
The str_random function generates a random string of the specified length.
If you don't want user could see generated random name, use hidden instead of text.

Laravel 5 - inserting old data into form via variable

I have a slight problem. I have a system whereby I can drag and drop my own forms. The html code for a form is saved in my database. When it comes to the edit page, I do something like the following
{!! Form::model($project->document, [
'class'=>'form-horizontal',
'method' => 'PATCH',
'route' => ['projects.documents.update', $project, $document->id]
]) !!}
{!! $documentData->documentData !!}
<div class="form-group">
{!! Form::submit('Save Data', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
$documentData->documentData contains the html code for this particular form.
Now my problem is, $documentData->form_data contains the old inputs for this form.
Is there any way to get this old input into the form, the way I am currently handling things?
Thanks
in controller you can access old input by $request->flash(); while in frontend you can access old by input type="text" name="name" value="{{ $name }}"

Why is my laravel 5 rendering html as string instead of dom?

So I have written the following route:
Route::get('/login', function() {
return View::make('login.form');
});
This is the view:
#extends('layouts.master')
#section('content')
<div class="form-section">
{{ Form::open(
array(
'url' => 'login-submit',
'method' => 'POST'
)
)
}}
{{ Form::submit('Authorize With AisisPlatform') }}
{{ Form::close() }}
#stop
This is exactly what I see when I look at the page:
<form method="POST" action="http://app-response.tracking/login-submit" accept-charset="UTF-8"><input name="_token" type="hidden" value="7xHzX20h1RZBnkTP2CRraZVsAfSQIfVP61mBiFtN"> <input type="submit" value="Authorize With AisisPlatform"> </form>
Um..... Shouldn't the form be well .... and actual form? Why did it render out the html as a string? How do I make it render the actual form submit button?
The default echo braces: {{ ... }} escape HTML by default, to prevent HTML injection.
You should use {!! .. !!} to print raw HTML. For example:
{!! Form::submit('Authorize With AisisPlatform') !!}

Prevent from submiting form several times by clicking fast in laravel

So I ran into issue, that If i click fast enough subnmit button, my form is submited several times. How can I prevent this? Token is added automatically, but it doesnt help at all I guess.
Form example:
<div class="row padding-10">
{!! Form::open(array('class' => 'form-horizontal margin-top-10')) !!}
<div class="form-group">
{!! Form::label('title', 'Title', ['class' => 'col-md-1 control-label padding-right-10']) !!}
<div class="col-md-offset-0 col-md-11">
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('body', 'Body', ['class' => 'col-md-1 control-label padding-right-10']) !!}
<div class="col-md-offset-0 col-md-11">
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-offset-5 col-md-3">
{!! Form::submit('Submit News', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
</div>
My NewsController store method:
public function store()
{
$validator = Validator::make($data = Input::all(), array(
'title' => 'required|min:8',
'body' => 'required|min:8',
));
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
News::create($data);
return Redirect::to('/news');
}
One approach is to use a click handler for the button where it first disables the button, then submits the form.
<script>
function submitForm(btn) {
// disable the button
btn.disabled = true;
// submit the form
btn.form.submit();
}
</script>
<input id="submitButton" type="button" value="Submit" onclick="submitForm(this);" />
Use PHP sessions to set a session variable (for example $_SESSION['posttimer']) to the current timestamp on post. Before actually processing the form in PHP, check if the $_SESSION['posttimer'] variable exists and check for a certain timestamp difference (IE: 2 seconds). This way, you can easily filter out multiple submits.
// form.html
<form action="foo.php" method="post">
<input type="text" name="bar" />
<input type="submit" value="Save">
</form>
// foo.php
if (isset($_POST) && !empty($_POST))
{
if (isset($_SESSION['posttimer']))
{
if ( (time() - $_SESSION['posttimer']) <= 2)
{
// less then 2 seconds since last post
}
else
{
// more than 2 seconds since last post
}
}
$_SESSION['posttimer'] = time();
}
Original POST
How to prevent multiple inserts when submitting a form in PHP?
Want to submit value of button as well and prevent double form submit?
If you are using button of type submit and want to submit value of button as well, which will not happen if the button is disabled, you can set a form data attribute and test afterwards.
// Add class disableonsubmit to your form
$(document).ready(function () {
$('form.disableonsubmit').submit(function(e) {
if ($(this).data('submitted') === true) {
// Form is already submitted
console.log('Form is already submitted, waiting response.');
// Stop form from submitting again
e.preventDefault();
} else {
// Set the data-submitted attribute to true for record
$(this).data('submitted', true);
}
});
});
Step 1: write a class name in the form tag Exp: "from-prevent-multiple-submits"
<form class="pt-4 from-prevent-multiple-submits" action="{{ route('messages.store') }}" method="POST">
#csrf
Step 2: write a class in button section
<button type="submit" id="submit" class="btn btn-primary from-prevent-multiple-submits">{{ translate('Send') }}</button>
Step 3: write this script code
(function(){
$('.from-prevent-multiple-submits').on('submit', function(){
$('.from-prevent-multiple-submits').attr('disabled','true');
})
})();

Categories