My PHP submit button does not work. How to solve it? - php

I am using Laravel 5 and I have some forms in one page. All is works except the last one. It is nothing happen, no error. I tried to put alert in the button. The alert showed, but the data won't saved.
<tr>
<div>
<form action="{{ url('AddComment') }}" method="POST">
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<button id ="af" class="btn btn-round btn-success">Submit</button>
</td>
</div>
</form>
</div>
</tr>

You need to have one submit button to submit a form until you submit it using jquery.
<tr>
<div>
<form action="{{ url('AddComment') }}" method="POST">
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="submit" value="Submit"> // This is SUBMIT button.
</td>
</div>
</form>
</div>
</tr>
Or,
If you want to keep the <button> as it is, then you should use jquery/JS function to submit it.

add button type too
<button id ="af" class="btn btn-round btn-success" type="submit">Submit</button>

You are not sending CSRF token in the form. Please use CSRF token in your form.
{ csrf_field() }}
Then you need to use
<input type="submit"/>
for submitting form.If you want to use
<button/>
to sbumit form,then you have to submit your form using jquery. Your form should look like.
<form action="{{ url('AddComment') }}" method="POST">
{ csrf_field() }}
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="submit" value="Submit">
</td>
</div>
</form>

Check these points:
1. Add { csrf_field() }} into your form
2. Change button to input type submit (<input type="submit"/>)
3. Check your modal and find out if all fields are fillable.
4. Print your query and check what sql query has been created.

Add CSRF:
<input type="hidden" value="{{csrf_token()}}" name="_token" id="token">
OR
{!! csrf_field() !!}
Good luck

Thank you for all the answer to helping me. I have tried it all but still nothing happen. But I looked at #JYoThI comment which said 'You can't place the form as a child element of table ,tbody, tr .' then I moved the form tags inside the <td> and the it is work !!
<td>
<form action="{{ url('AddComment') }}" method="POST">
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<button id ="afjk" class="btn btn-round btn-success">Submit</button>
</form>
</td>

Check #af on your JavaScript, maybe you put preventDefault() on it. Because if not, every posted answer should have solved your problem by now

Related

How to shift _token to the last of the url in laravel

Hello I want to shift the _token in the last of URL asshown below
What I am getting:
example.com/search?_token=qkPc5aNyEp7tysbyQhZcnjHdP1wi9q&query=php
What I want:
example.com/search?query=php&_token=qkPc5aNyEp7tysbyQhZcnjHdP1wi9q
My Form code Is like
<form action="search" method="GET">
{!! csrf_field() !!}
<div class="main-search-input fl-wrap">
<div class="main-search-input-item">
<input type="text" name="query" value="" placeholder="Search snippets..." required>
</div>
<button class="main-search-button" type="submit">Search</button>
</div>
</form>
Put the csrf field at the bottom of the form tho I think you don't need it.

post route in Laravel

I have done everything the right way but my submit button doesnt do anything and I dont know why....
Here is my view
<form action="{{ route('importUser') }}" method="POST" enctype="multipart/form-data">
#csrf
add users via excell<input name="file" class="form-control" style="padding-bottom:3em; margin-bottom:3em" type="file">
<div style="display:inline;">
<input type="submit" class="btn btn-primary btn-lg" value="ارفع" >
</div>
</form>
Here is my controller
function importUser(Request $request)
{
#code...
}
and my route
Route::POST('ImportUsersFile', 'ExcelUserController#importUser')->name('importUser')->middleware('Admin');
Apparently, the flow dont get in the function import user. I tried to dd into it but nothing happend!
According to an error message you provided in a comment, try this:
php artisan key:generate
Try using url intead of route
<form action="{{ url('ImportUsersFile') }}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
add users via excell<input name="file" class="form-control" style="padding-bottom:3em; margin-bottom:3em" type="file">
<div style="display:inline;">
<input type="submit" class="btn btn-primary btn-lg" value="ارفع" >
</div>
</form>
And in your routes:
Route::post('ImportUsersFile', ['uses' => 'ExcelUserController#importUser', 'as' => 'importUser']);

Form clears parameter from given action url - PHP Laravel

<form role="search" method="get" action="{{ route('people.search', array_replace(Request::all(), [" type" => "customers"])) }}">
<div class="input-group">
<input type="text" name="search" class="form-control" placeholder="Search...">
<div class="input-group-btn">
<button class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
The following form action link looks like this:
http://127.0.0.1:8080/people/search/customers?countries=Germany&statuses=3&page=1
But when I submit the search form, it redirects to:
http://127.0.0.1:8080/people/search/customers?search=test
Instead of including the parameters that were previously set.
Is there a way to prevent that?
What I did to include the previous parameter is just by adding them as hidden fields:
#foreach(request()->query() as $key => $value)
#if($key == "page") // Don't want the page parameter to be in there
#continue
#endif
<input type="hidden" name="{{$key}}" value="{{$value}}">
#endforeach

Laravel delete item from table list / db

I am trying to delete item from a generated table of items which are from a database table.
My Route:
Route::delete('destroy/{deviceID}', ['as' => 'destroyDevice', 'uses' => 'DeviceController#destroyDevice']);
My Controller method to delete an item:
public function destroyDevice(Request $request, $deviceId = 0)
{
$device = Device::find($deviceId);
if($device)
{
$device->delete();
return redirect()->route('index')->with('success', 'Erfolgreich gelöscht');
}
else
{
return redirect()->route('index')->with('error', 'Fehler');
}
}
And my blade template:
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<td>
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</td>
</form>
If I click on the button nothing happens no error no Response, what am I doing wrong.
If I click on the third delete button the form holds this:
<form action="http://localhost/app/public/device/destroy/3" method="post" name="delete_device"></form>
You can solve this by putting the form inside a td tag in that table.
Like this:
<td> <!-- <--- put these -->
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</td> <!-- <--- put these -->
I think the form gets ignored somehow due to not being valid, but I am not 100% sure. May people edit this answer ;)
The parameter is case sensitive so it should be deviceID instead of deviceId
public function destroyDevice(Request $request, $deviceID = 0)
Maybe you have some script that prevents the form to submit, some prevent default maybe on button click or on form submit. Check that.

Laravel foreach loop not outputting to form

Here is a form from my Laravel 5 project:
<form class="form-group" action="/counselors/{{ $counselor->id }}/badges/add" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($badges as $badge)
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" style="float: left" value="{{ $badge->id }}"> {{ $badge->name }}
</span>
</div>
#endforeach
<br>
<input type="submit" class="form-control btn btn-primary" name="submit" value="Submit"><hr>
<input type="button" class="form-control btn btn-danger" name="cancel" value="Cancel" onClick="location='/counselors'">
</form>
The $badges is a collection of badges from a database. They all iterate and display properly, but when i look at the request data with
$request->all()
it returns:
{"_token":"5E4csIJU4YVVQZSoPG1EmpfHfjNjYcRwuOoreCcE","submit":"Submit"}
It doesn't include the checkbox data from the loop. Is this incorrect? What's the best way to do this? There are 160 items in the $badges collection.
I'm pretty new so please go easy.
put name attribute to the input checkbox and giv it a value
<input type="checkbox" name="check-{{ $badge->id }}" style="float: left" value="{{ $badge->id }}"> {{ $badge->name }}
Note :
here i use the name "check-" and the id of the badge to get defferent name for each checkbox

Categories