i want to add new law details with a form, this form has two buttons like update and cancel.
when i hit on cancel button the form gets cancelled and is redirected to previous page, and when i hit update button without filling form details it asks for validation.
after filling all required details of form, when i hit update button it throws me error as MethodNotAllowedHttpException in RouteCollection.php line 218:
my form is this:
{!! Form::open(array('url' => 'admin/companymaster/updateLocation', 'id'=>'updatelocations', 'class' => 'form-horizontal create_form', 'files' => true)) !!}
<input type="hidden" value="{{$value->id}}" name="company_master_id">
<div id="sublaws_content1"></div>
<div id="save" style="display:none; margin-bottom: 20px;">
<button type="submit" class="btn btn-success" id="senddatepicker">Update</button>
<a class="btn red" href="{{ URL::to('admin/companymaster/'.$company_master->id) }}">Cancel</a>
</div>
</form>
my route is:
Route::post('admin/companymaster/updatelocations', 'CompanyController#updateLocation');
my controller is:
public function updateLocation(){
//dd(Input::all());
/*Insert Company Law Details*/
$companydetails_id = Input::get('company_master_id');
$company_sub_laws = Input::get('company_sub_laws');
if($company_sub_laws!="")
{
$cld=0;
foreach($company_sub_laws as $rescompany_sublaws)
{
if($companydetails_id!="" && $rescompany_sublaws!="")
{
$law_reg_no = $_POST['law_reg_no'];
$sub_law_start_date = $_POST['sub_law_start_date'][$cld];
$sub_law_end_date = $_POST['sub_law_end_date'][$cld];
$companylawdetails = new CompanyLawDetails;
$companylawdetails->company_master_details_id = $companydetails_id;
$companylawdetails->company_sub_law_id = $rescompany_sublaws;
$companylawdetails->law_reg_no = $law_reg_no;
$company_sub_law_start_date = $sub_law_start_date;
$company_sub_law_end_date = $sub_law_end_date;
if($company_sub_law_end_date!="")
{
$res_company_sub_law_end_date=explode("-",$company_sub_law_end_date);
$company_sub_law_end_date=$res_company_sub_law_end_date[2]."-".$res_company_sub_law_end_date[1]."-".$res_company_sub_law_end_date[0];
$companylawdetails->law_end_date = $company_sub_law_end_date;
}
if($company_sub_law_start_date!="")
{
$res_company_sub_law_start_date=explode("-",$company_sub_law_start_date);
$company_sub_law_start_date=$res_company_sub_law_start_date[2]."-".$res_company_sub_law_start_date[1]."-".$res_company_sub_law_start_date[0];
$companylawdetails->law_start_date = $company_sub_law_start_date;
}
$companylawdetails->save();
}
$cld++;
}
}
$sublaws = CompanyLawDetails::where('company_master_details_id',$companydetails_id)->select('company_sub_law_id')->get()->toArray();
$sublaws = join(",",array_column($sublaws,'company_sub_law_id'));
$update = CompanyDetails::where('id',$companydetails_id)->update(['company_sub_laws' => $sublaws]);
//return Redirect::back();
}
when i submit my form by clicking on update button i get an error page as:
can anyone help me out with this.?
It seems that you are going on the wrong route, and you have an error on url, try this way;
Route::post('admin/companymaster/updatelocations', ['uses'=>'CompanyController#updateLocation',
'as'=>'upload.locations']);
on your blade
{!! Form::open(array('route'=> 'upload.locations', 'method'=>'post', 'id'=>'updatelocations', 'class' => 'form-horizontal create_form', 'files' => true)) !!}
<input type="hidden" value="{{$value->id}}" name="company_master_id">
<div id="sublaws_content1"></div>
<div id="save" style="display:none; margin-bottom: 20px;">
<button type="submit" class="btn btn-success" id="senddatepicker">Update</button>
<a class="btn red" href="{{ URL::to('admin/companymaster/'.$company_master->id) }}">Cancel</a>
</div>
</form>
Please add form method as 'method' => 'post',
{!! Form::open(array('url' => 'admin/companymaster/updateLocation', 'id'=>'updatelocations', 'class' => 'form-horizontal create_form', 'files' => true,'method' => 'post')) !!}
Hope you understand.
Spelling error change your url form
{!! Form::open(array('url' => 'admin/companymaster/updateLocation', 'id'=>'updatelocations', 'class' => 'form-horizontal create_form', 'files' => true)) !!}
to
{!! Form::open(array('url' => 'admin/companymaster/updatelocations', 'id'=>'updatelocations', 'class' => 'form-horizontal create_form', 'files' => true)) !!}
Related
Hi all am trying to do validations for a text field which is depends on other selected value .there is two columns buyer and reference number.so the validation is when i select particular names in buyer column then the reference must be required.
Below is my code
model::
public static $rules = array(
'delivery_contact_no' => 'required|numeric',
'external_ref_number' => 'required',
);
controller::
$check = false;
if ($_POST["user"] == '11Street') {
$check = true;
if (Input::get('external_ref_number') == "") {
$rules = array(
'external_ref_number' => 'required'
);
$messages = array(
'external_ref_number.required' => 'The External Reference Number Must be required ',
);
}
if ($check) {
$validator = Validator::make(Input::all(), $message);
if ($validator->fails()) {
return Redirect::back()->withInput()->withErrors($validator);
}
}
}
I am inserting input data like below:
$get = array(
'user' => trim($_POST["user"]),
'external_ref_number' => trim(Input::get('external_ref_number')),
);
so the validation is when I select particular names in buyer column then the reference must be required. below is view code
view::
<div class="form-group #if ($errors->has('user')) has-error #endif">
{{ Form::label('user', 'Buyer *', array('class'=> 'col-lg-2 control-label')) }}
<div class="col-lg-4">
{{ Form::text('user', Input::old('user'), array('class'=> 'form-control', 'autofocus' => 'autofocus', 'readonly' => 'readonly')) }}
{{ $errors->first('user', '<p class="help-block">:message</p>') }}
<input type="hidden" id="user_id" name="user_id">
</div>
<div class="col-xs-1">
<div class="input-group">
<div class="input-group-btn">
<span class="pull-left"><button id="selectUserBtn" class="btn btn-primary selectUserBtn" href="/transaction/ajaxcustomer"><i class="fa fa-plus"></i> Select Buyer</span>
</div>
</div>
</div>
</div>
<div class="form-group required {{ $errors->first('external_ref_number', 'has-error') }}">
{{ Form::label('external_ref_number', 'External reference number ', array('class' => 'col-lg-2 control-label')) }}
<div class="col-lg-4">
{{ Form::text('external_ref_number', $orderMappedInfo['external_ref_number'], ['placeholder' => 'External reference number', 'class' => 'form-control']) }}
{{ $errors->first('external_ref_number', '<p class="help-block">:message</p>') }}
</div>
</div>
The validation code which I wrote is not working. Please help me to solve this issue.
I am trying to do validations for a text field which is depends on other selected value.
There are two columns buyer and reference number.
so the validation is when i select particular names in buyer column then the reference must be required.
My view is like this :
#foreach($users as $user)
<tr>
<td>{!! $user->id !!}</td>
<td>{!! $user->username !!}</td>
<td>{!! $user->phone !!}</td>
<td>{!! $user->address !!}</td>
<td>
{!! Form::open(['route' => ['users.destroy.year', $user->id, $year], 'method' => 'delete']) !!}
<div class='btn-group'>
</i>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
#endforeach
My routes\web.php is like this :
Route::get('users/destroy/{year}', 'UserController#destroy')->name('users.destroy.year');
Route::resource('users', 'UserController');
My controller is like this :
public function destroy($id, $year)
{
$user = $this->userRepository->findWithoutFail($id);
if (empty($user)) {
Flash::error('User not found');
return redirect(route('users.index.year', ['year' => $year]));
}
$this->userRepository->delete($id);
Flash::success('User deleted successfully.');
return redirect(route('users.index.year', ['year' => $year]));
}
There is exist error like this :
MethodNotAllowedHttpException in RouteCollection.php line 218:
And the url looks like this : http://localhost/mysystem/public/users/2?2016
When click button delete, I want the url looks like this : http://localhost/mysystem/public/users/index/2016
Is there any people who can help me?
In your view you have declared the form method as DELETE; but in the routes\web.php file, you have declared the route as GET
Change the view
{!! Form::open(
['route' => ['users.destroy.year', $user->id, $year],
'method' => 'get']
)!!}
Your routes/web.php should look like so,
Route::get('users/index/{year}', 'UserController#index')
->name('users.index.year');
Route::get('users/destroy/{id}/{year}', 'UserController#destroy')
->name('users.destroy.year');
Route::resource('users', 'UserController', ['except' => ['index', 'destroy']]);
Keep your controller code as it is.
This should do the trick.
Just a suggestion
It's better not to keep delete operation on GET request. So change the DELETE route like so
Route::delete('users/destroy/{id}/{year}', 'UserController#destroy')
->name('users.destroy.year');
Change the view
{!! Form::open(
['route' => ['users.destroy.year', $user->id, $year],
'method' => 'delete']
)!!}
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
id is not declared in route and may be this is the cause, So change you route:
Route::get('users/destroy/{year}', 'UserController#destroy')->name('users.destroy.year');
to this
Route::get('users/destroy/{id}/{year}', 'UserController#destroy')->name('users.destroy.year');
Hope this solve this cause.
Im new to laravel and i want to make an update function but it doesn t work. I get my update form correctly but when i submit it i get an error that says it can t updata a parent/child row
This is my function
public function update($verhaal, $wereld)
{
$wereld = Werelden::where('wereld_id', $wereld)->update([
'wereld_id' => $wereld,
'naam' => 'naam',
'beschrijving' => 'beschrijving',
'verhaal_id' => 'verhaal_id'
]);
return redirect('/');
}
This are my routes
Route::get('verhalen/{verhaal}/bekijken/{wereld}/edit', 'WereldController#BewerkWereld');
Route::put('verhalen/{verhaal}/bekijken/{wereld}/edit', 'WereldController#update');
And as last i have my html form
<div class="panel-body">
#include('common.errors')
<form action="{{ url('/verhalen/'. $wereld->verhaal_id .'/bekijken/'. $wereld->wereld_id .'/edit')}} " method="POST">
{{ csrf_field() }}
{{ method_field('PUT') }}
<input type="text" class="form-control verhaal-toevoegen" value="{{ $wereld->naam }}" name="naam">
<textarea class="form-control" id="verhaal-text" name="verhaal">{{ $wereld->beschrijving}}</textarea>
<button type="submit" class="btn btn-primary">Verhaal bijwerken</button>
</form>
</div>
I hope someone can help me
public function update($verhaal, $wereld)
{
$wereld = Werelden::where('wereld_id', $wereld)->update([
'wereld_id' => $wereld,
'naam' => 'naam',
'beschrijving' => 'beschrijving',
'verhaal_id' => $verhaal_id // "verhaal_id" It is not an id
]);
return redirect('/');
}
uses columns necesaries only
$wereld = Werelden::where('wereld_id', $wereld)->update([
'wereld_id' => $wereld,
'naam' => 'naam',
'beschrijving' => 'beschrijving',
]);
I have a blade form in Laravel
{!! Form::open(array('url' => '/'.$cpe_mac.'/device/'.$device_mac.'/update', 'class' => 'form-horizontal', 'role' =>'form','method' => 'PUT')) !!}
<span class="pull-left">
<div class="col-sm-6">
<p>Downlink </p>
<select type="text" class="form-control" name="max_down" >
#foreach ($rate_limit as $key => $value)
<option value="{{$value or ''}}">{{$value or ''}} Kbps</option>
#endforeach
</select>
</div>
<div class="col-sm-6">
<p>Uplink</p>
<select type="text" class="form-control" name="max_up" >
#foreach ($rate_limit as $key => $value)
<option value="{{$value or ''}}">{{$value or ''}} Kbps</option>
#endforeach
</select>
</div>
</span><br>
{!! Form::hidden('cpe_mac', $cpe_mac)!!}
{!! Form::hidden('device_mac', $device_mac)!!}
<span class="pull-right">
<button class="saveRateLimitBTN btn btn-xs btn-info pull-right">Save</button>
</span>
{!! Form::close();!!}
I want to be make a Ajax HTTP PUT without refresh my page.
How should my form look like ?
Right now, it keep redirecting me to url/update, and then redirecting it back.
How do I send all those data to my controller in the background ?
Any hints / suggestion on this will be a huge helps !
Usually I place my forms in a modal popup with a data-dismiss button that will simply close after you hit submit and run everything in the background.
For your case, after hitting submit without refreshing the page after hitting the submit button would seem a bit odd wouldn't it?
However here's a bit of jQuery to get you going either way:
$("#mySubmitButton").on('click', function(event){
var form = '#myForm';
$.post($(form).attr('action'),$(form).serialize());
});
Add an ID to your form:
{!! Form::open(array('url' => '/'.$cpe_mac.'/device/'.$device_mac.'/update', 'class' => 'form-horizontal', 'role' =>'form','method' => 'PUT', 'id' => 'myForm')) !!}
Add an ID to your button:
<button id="mySubmitButton" class="saveRateLimitBTN btn btn-xs btn-info pull-right">Save</button>
Add a TYPE to your button (to prevent it from submitting/refreshing);
<button type="button" id="mySubmitButton" class="saveRateLimitBTN btn btn-xs btn-info pull-right">Save</button>
If your routes are properly setup to lead to your controller then all your form input variables are now accessible via $request or Input:
public function processForm(Request $request)
{
$variable1 = $request->input('myforminputname1');
$variable2 = $request->input('myforminputname2');
// Or get it through Input
$variable1 = \Input::get('myforminputname1');
$variable2 = \Input::get('myforminputname2');
}
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');
})
})();