form displaying in controller popup - laravel-5

i make form that i want to link in with controller called companies controller but i add form code inside the list it show the whole code ink the web page
<li>
<a href="#" onclick="
var result = confirm('are you sure you want to delete this project?');
if(result){
event.preventDefault();
document.getElementById('delete-form').submit();
}
">
Delete
</a>
<form id="delete-form" action="{{ route('companies.destroy',[$company->id]) }}" method="post" style="display:none;">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
</form>
</li>
in the web page only the delete should visible in the section not other part of the code

first of all, separate your code of javascript from html so that it be understandable and also easy to debug. try this:
<li>
Delete
<form id="delete-form" action="{{ route('companies.destroy',[$company->id]) }}" method="post" style="display:none;">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
</form>
</li>
<script type="text/javascript">
function delete()
{
var result = confirm('are you sure you want to delete this project?');
if(result){
event.preventDefault();
document.getElementById('delete-form').submit();
}
}
</script>
Found some typos which I corrected, maybe that was the issue. Also, you could very well send an ajax instead of submitting a hidden form, just an idea.

Related

How to make edit and delete buttons hidden if the user didn't create the content

I have an article , and I put edit , delete buttons but If I create another user I can delete the article.
this is my code :
#auth
<div class="card-body">
<div class="align-items-center">
{{__("Edit")}}
<form method="post" action="{{route('articles.destroy', $article)}}" style="display: inline-block">
#method('DELETE')
#csrf
<button onclick="return confirm('{{__("Are you sure you want to delete this article ?")}}')" class="btn btn-danger">{{__("Delete")}}</button>
</form>
</div>
#endauth
I would suggest using the article user id. If you don't track this, add a field to the Article model, user_id, which would show who wrote the article. Then, you can easily create an if-check to make sure only the writer can delete or edit his own article.
On your blade page then:
#if($article->user_id === \Auth::user()->id)
<div class="align-items-center">
{{__("Edit")}}
<form method="post" action="{{route('articles.destroy', $article)}}" style="display: inline-block">
#method('DELETE')
#csrf
<button onclick="return confirm('{{__("Are you sure you want to delete this article ?")}}')" class="btn btn-danger">{{__("Delete")}}</button>
</form>
</div>
#endif
And add a similar verification in your controller's destroy() method:
if($article->user_id === \Auth::user()->id)
// Do the delete
else
// return no-auth
Don't forget to add the current user to the $article when you save it (in the store() method of the controller):
$article->user_id = \Auth::user()->id
$article->save();
HTH

pass input value to url delete method laravel

i have form in my delete modal, and i want to pass hidden input to url method delete/destroy
this is my delete modal :
<form action="{{url('/pages').'/' ... }}" method="POST" enctype="multipart/form-data">
{{ method_field('DELETE') }}
{{csrf_field()}}
<div class="form-group">
<label for="">Are you sure to delete this Page ?</label>
<input type="text" class="form-control" id="page_id" style="display: none;">
</div>
<button type="submit" class="btn btn-danger" id="save">Delete</button>
<button type="button" class="btn btn-link" data-dismiss="modal">{{__('Close')}}</button>
</form>
how to append input value(page_id) to form action ?
so i can use that page_id in form action :
<form action="{{url('/pages').'/'page_id}}" method="POST" enctype="multipart/form-data">
can some one help me ? thanks before :)
You have to add a new attribute on each delete button contains the id.
please check the following code
$('.delete-button').click(function(){
var pageId = $(this).data('page-id');
$('.page_id').html(pageId);
$('#modal-form').attr('action', deleteUrl+pageId);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
page 1
page 2
page 3
<form action="#" id="modal-form" method="POST" enctype="multipart/form-data">
<label for="">Are you sure to delete this Page <span class="page_id"></span>?</label>
</form>
<script>
/* window.deleteUrl = "{{url('/pages').'/'}}"; // save globally */
window.deleteUrl = "http://test.com/"; // temporary
</script>
If you are using boostrap modal then you have to pass data from that button something like this,
<button id="submit-btn" data-action="{{route('test')}}">Submit</button>
In JS,
replace modal's action with data-action.
Something like this,
$('#form-id').val($('#submit-btn').data('action))
And if you are not using modal then just store that id in variable and pass in route directly.
Hope it helps :)

Deleting record in Laravel

Below is my code which I have written to delete data from MySql using laravel but I am facing a problem while deleting; which is it always delete the top most row; regardless of which row I clicked.
Edit
<a href="#" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form').submit();
}
">
Delete
<form id="delete-form" action="{{ route('posts.destroy',[$post->id]) }}" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>
Here is my controller file:
public function destroy(Post $post)
{
dd($post->id); same id comes here always rregardless of which row I might click...
$findpost = Post::find($post->id);
if($findpost->delete()){
return redirect('/posts')->with('success','Post Removed Successfully') ;
}
return back()->withinput()->with('error',"Company Post be deleted");
}
It's because of you define same ID for all your fomrs.
You can define unique form id by adding POST ID at the end of it.
So, your code would be:
Edit
<a href="#" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form{{$post->id}}').submit();
}
">
Delete
<form id="delete-form{{$post->id}}" action="{{ route('posts.destroy',[$post->id]) }}" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>
The problem you are facing is because you are referencing to the same form every single time. ID in HTML is unique for an element, and it will always reference to one element randomly in your case always the first element with that ID on the page.
A better approach would be to have the url for each element in the anchor tag:
<a href="{{ route('posts.destroy',[$post->id]) }}" onclick="
var result = confirm('Are you Sure, You want to delete this Company?');
if(result){
document.getElementById('delete-form').setAttribute("action", this.href).submit();
}
return false; // to prevent submitting the form on click before the alert is displayed
">
and have one <form> element in your view:
<form id="delete-form" action="" method="post" style="display:none;" >
<input type="hidden" name="_method" value="delete" >
{{csrf_field()}}
</form>

Method Not Allowed HTTP Exception in Laravel 5.4

I have a form with two text fields and two buttons (edit and delete), when I press edit button, it works fine but when I press delete button, it gives the 'MethodNotAllowedHttp' exception. My code is as follows:
<form action="/laboratory/doctors/update" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" aria-describedby="name" value="{{ $doctor->name }}">
</div>
<div class="form-group">
<label for="percentage">Percentage:</label>
<input type="text" class="form-control" id="percentage" value="{{ $doctor->percentage }}">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
Delete
</form>
My Routes are as follows:
Route::post('/doctors/update', 'DoctorsController#update');
Route::delete('/doctors/{doctor}/delete', 'DoctorsController#destroy');
Any help is appreciated.
You have created route of http verbs as delete type and trying to get of type get. So you can change it as get instead delete
Route::get('/laboratory/doctors/{doctor}/delete', 'DoctorsController#destroy');
Also there is other way to make http verb as DELETE but using
Another form tag outside of current form tag.
Or use ajax to make it delete type
Href on an anchor html element will result in a GET call but your route expect a Delete call. You have some ways to make sure you will result in a delete call.
One of the most common ways is to use a form instead to post data to your server.
DELETE
{{ Form::open(['url' => '/laboratory/doctors/{{ $doctor->id }}/delete', 'method' => 'DELETE']) }}
{{ Form::button('delete', ['type' => 'submit',
'class' => 'btn btn-danger']) }}
{{ Form::close() }}
For best practise I recommend to use {{ Form::open(...) }} {{ Form::close() }} only once and refactor your controller code so it can read the value from the buttons and translate that in the corresponding {doctor} of the delete post so you dont have multiple html forms in your code.
#if(isset($doctor))
<form action="/laboratory/doctors/{{$doctor->id}}/delete" method="POST">
#else
<form action="/laboratory/doctors/update" method="POST">
#endif
{{ csrf_field() }}
#if(isset($doctor))
{{ method_field('DELETE') }}
#endif
<div class="form-group">
<label for="name">Name:</label>
<input type="text" class="form-control" id="name" aria-describedby="name" value="{{ $doctor->name }}">
</div>
<div class="form-group">
<label for="percentage">Percentage:</label>
<input type="text" class="form-control" id="percentage" value="{{ $doctor->percentage }}">
</div>
<button type="submit" class="btn btn-success">Save Changes</button>
Delete
</form>
The problem is in your action.. when you click delete button, the button run the destroy function, but the form run the update function. So, you have to separate the form action, or you can delete it by using ajax

How to send post request without submit button

I have form where I don't have submit button and actually the form didn't send post request, so my question how is possible to send request on image click?
#foreach($items as $item)
<form class="form-horizontal" role="form" method="post" action="{{ route('index', $item->id) }}">
{{ csrf_field() }}
<div class="col-md-3">
<div class="well">
<a><img class="img-responsive center-block" src="{{ $item->imagePath }}" alt=""></a>
<h4 class="text-center">{{ $item->item_name }}</h4>
</div>
</div>
</form>
#endforeach
Thank you
You can do it with jQuery. It's easy if you set id of the form:
$('form#form_id a').click(function(e){
$('form#form_id').submit();
e.preventDefault();
return false;
});
But in your case you don't have any point in submitting post request, but anyway the above should work.
Assuming that "Don't use a submit button" is not a hard requirement and you are just assuming that it is because you want to use an image:
Use a submit button instead of a link.
<button><img class="img-responsive center-block" src="{{ $item->imagePath }}" alt=""></button>
You can style the default background and borders of the button away with CSS.
You really should have some content in the alt attribute. The image can't be a meaningless decorative image if it informs the user that clicking it will do something.
give id to your form in this way
<form action="" id="frm-id" method="post" class="form-horizontal ">
next you js method will be as
$("#frm-id").submit(function(){
var form_data = $("#frm-id").serialize();
$.ajax({
url: '/index/'+id,
type: "POST",
});
});
At url above you need to your route to go in controller method as i wrote....

Resources