laravel throwing MethodNotAllowedHttpException for validation - php

I tried to create a form with post method. When there was validation in the method, I faced with "Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException" , but when i deleted validation it worked correctly. I don't know why!
HTML
#if(count($errors)>0)
<ul>
#foreach($errors->all() as $error)
<li style="color: #cc5200;"><i class="fas fa-times"></i>{{$error}}</li>
#endforeach
</ul>
#endif
<form method="post" action="{{route('test.added')}}">
#csrf
<input type="hidden" name="grade" value="{{$grade}}">
<input type="hidden" name="course" value="{{$course}}">
<input type="hidden" name="numbers" value="{{$numbers}}">
#for($i=0;$i<$numbers;$i++)
<div>
<span style="color: #cc5200">question{{$i+1}} :</span>
<textarea name="question[]" rows="2" style="width: 50vw">{{old('question')[$i]}}</textarea>
</div>
<div class="d-flex flex-row">
<div class="ml-1 mr-2">
<span>1-</span>
<input type="text" name="firstOption[]" style="width: 10vw" value="{{old('firstOption')[$i]}}">
<input type="radio" name="trueOption[{{$i}}]" value="1">
</div>
<div class="ml-1 mr-2">
<span>2-</span>
<input type="text" name="secondOption[]" style="width: 10vw" value="{{old('secondOption')[$i]}}">
<input type="radio" name="trueOption[{{$i}}]" value="2">
</div>
<div class="ml-1 mr-2">
<span>3-</span>
<input type="text" name="thirdOption[]" style="width: 10vw" value="{{old('thirdOption')[$i]}}">
<input type="radio" name="trueOption[{{$i}}]" value="3">
</div>
<div class="ml-1 mr-2">
<span>4-</span>
<input type="text" name="forthOption[]" style="width: 10vw" value="{{old('forthOption')[$i]}}">
<input type="radio" name="trueOption[{{$i}}]" value="4">
</div>
</div>
<br>
#endfor
<input type="submit" value="ok" style="float: left;color: white;background: #cc5200">
</form>
web.php
Route::post('/td-admin/test/add-finalStage','TestController#added')->name('test.added');
TestController
public function added(Request $request){
$request->validate([
'question.*'=>'required',
'firstOption.*'=>'required',
'secondOption.*'=>'required',
'thirdOption.*'=>'required',
'forthOption.*'=>'required',
'trueOption.*'=>'required',
]);
Test::create( ...
It made me crazy. please help me.

Related

PHP - How to get the radio check buttons underneath without the wide distance?

I would like to have those two radio check buttons underneath without the wide distance. Not sure what I do wrong here.. any help would be appreciated!
<div class="row ms-3 me-3">
<div class="col-12">
<section class="admin admin-simple-sm p-3 card-shadow">
<form method="POST" style="text-align: left;" action="{{url('/homepreference/' . auth()->user()->id)}}" enctype="multipart/form-data">
{{ method_field('PUT') }}
#csrf
<span class="h6"> #lang('messages.new.homepreference') : </span>
<br>
<div class="form-check form-check-inline ms-4">
<label class="form-check-label" for="homepage1">
<input class="form-check-input"
#if (auth()->user()->homepage == '0')
checked="checked"
#endif
type="radio" name="homepage" id="homepage1" value="0"> <span class="h6"> #lang('messages.new.viewallposts') </span>
</label>
</div>
<br>
<div class="form-check form-check-inline">
<label class="form-check-label" for="homepage2">
<input class="form-check-input"
#if (auth()->user()->homepage == '1')
checked="checked"
#endif
type="radio" name="homepage" id="homepage2" value="1">
<span class="h6">#lang('messages.new.viewfollowingposts') </span>
</label>
</div>
<br>
<button type="submit" class="btn btn-sm btn-primary">#lang('messages.save')</button>
</form>
</section>
</div>
</div>
Add style="text-align: left;" to the form
like this:
<form method="POST" style="text-align: left;" action="{{url('/homepreference/' . auth()->user()->id)}}" enctype="multipart/form-data">

Http request 500 internal server error on Axios put

I have an issue submitting my updated data to database.
It accepts all input but when I'm trying to get it saved it gets the error.
This is my route:
Route::resource('Sales', 'SalesController');
How I put my data:
axios.put('/Sales/' + id)
My update method:
public function update(Request $req, $id){
$sales = Sales::find($id);
$sales = new Sales;
$sales->itemQty = $req['itemQty'];
$sales->itemID = $req['itemID'];
$sales->save();
}
My Editing form:
<div class="card-body" v-for="(sale, index) in sales" :key="sale.id">
<form action="/Sales" method="post">
<div>
<input type="hidden" name="_token" :value="csrf">
<div class="form-group">
<div class="form-inline">
<div class="mr-3 mt-2">Item ID: </div>
<input class="form-control mt-2 col" name="itemID[]" maxlength="12" v-model="sale.itemID">
</div>
<div class="form-inline">
<div class="mr-2 mt-2">Quantity: </div>
<input type="number" min="1" class="form-control mt-2 col" placeholder="Quantity" name="itemQty[]" v-model="sale.itemQty">
</div>
</div>
</div>
<div class="updatesale" style="margin:0 auto; text-align: center;">
<button class="btn btn-success mt-2" type="button" name="button" v-on:click="updateSalesRec(sale.id)">Update</button>
</div>
</form>
</div>
What is wrong with it?
You don't need to create another instance of the Sales object after finding it:
public function update(Request $req, $id){
$sales = Sales::find($id);
$sales->itemQty = $req['itemQty'];
$sales->itemID = $req['itemID'];
$sales->save();
}
I don't think your intention is to have your data submitted in array. So remove [] from the input field names:
<div class="card-body" v-for="(sale, index) in sales" :key="sale.id">
<form action="/Sales" method="post">
<div>
<input type="hidden" name="_token" :value="csrf">
<div class="form-group">
<div class="form-inline">
<div class="mr-3 mt-2">Item ID: </div>
<input class="form-control mt-2 col" name="itemID" maxlength="12" v-model="sale.itemID">
</div>
<div class="form-inline">
<div class="mr-2 mt-2">Quantity: </div>
<input type="number" min="1" class="form-control mt-2 col" placeholder="Quantity" name="itemQty" v-model="sale.itemQty">
</div>
</div>
</div>
<div class="updatesale" style="margin:0 auto; text-align: center;">
<button class="btn btn-success mt-2" type="button" name="button" v-on:click="updateSalesRec(sale.id)">Update</button>
</div>
</form>
</div>
Hope this is useful.

on update method getting error

I am trying to perform UPDATE operation but getting error Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException. Getting this error when I press the UPDATE button for data update. I can not understand the error reason. Please help!!!
controller block update method
public function update(Request $request, $id)
{
$book = Book::find($id);
$book -> BookName = $request->get('NBookName');
$book -> BookID = $request->get('NBookId');
$book -> BookUnitPrice = $request->get('NBookUnitPrice');
if($book->save())
{
return view('pages.book', $this->fetchData())->with('alert-success', 'books updated successfully.');
}
else
{
return redirect()->back()->with('alert-success',$error->getMessage());
}
}
update page
<form class="form-horizontal" method="POST" action="{{action('BookController#update', $id)}}" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH"/>
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" value="{{$book->BookName}}" maxlength="100" minlength="3" autofocus="autofocus" autocomplete="off" required="required" name="NBookName" class="form-control"/>
</div>
</div>
<div class="col-md-4">
<div class="form-group" style="padding-left: 5%;">
<label>Book ID</label><span class="required">*</span>
<input type="text" value="{{$book->BookID}}" maxlength="10" minlength="1" autocomplete="off" required="required" name="NBookId" class="form-control"/>
</div>
</div>
<div class="col-md-4">
<div class="form-group" style="padding-left: 5%;">
<label>Unit Price</label><span class="required">*</span>
<input type="text" value="{{$book->BookUnitPrice}}" maxlength="5" required="required" autocomplete="off" runat="server" name="NBookUnitPrice" class="form-control" onkeypress="return decimalOnly(event)"/>
</div>
<div class="form-group" style="padding-left: 5%;">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</form>
Route list
// for books
Route::get('/book','BookController#create');
Route::post('/book','BookController#store');
Route::get('/book/{id}','BookController#edit');
Route::patch('/book/{id}', 'BookController#update');
I updated the route method from PUT to PATCH, it worked. Now the data is updating. But the success message in UPDATE is not printing. Please help!!!
Please see the image attached for the error page.
You either have to change
<input type="hidden" name="_method" value="PATCH"/>
to
<input type="hidden" name="_method" value="PUT"/>
or change
Route::put('/book/{id}', 'BookController#update');
to
Route::patch('/book/{id}', 'BookController#update');

Bootstrap multiple choice

Im trying to bootstrap a mulitple choice tickbox for PHP. I have the following code:
<div class="container">
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<span class="input-group-addon">
<form action="catcheck.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="submit" />
</form>
</span>
</div>
</div>
</div>
</div>
But im just getting a normal checkbox, how do I go about changing this to submit to catcheck.php whilst using the bootstrap css?
The below code will work (Hope this is what you needed):
<div class="container">
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<span class="input-group-addon">
<form action="catcheck.php" method="post" role="form">
<label>
<input type="checkbox" name="check_list[]" value="value 1" /> Value 1
</label>
<label>
<input type="checkbox" name="check_list[]" value="value 2" /> Value 2
</label>
<label>
<input type="checkbox" name="check_list[]" value="value 3" /> Value 3
</label>
<label>
<input type="checkbox" name="check_list[]" value="value 4" /> Value 4
</label>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</span>
</div>
</div>
</div>
</div>
In catcheck.php
You can retrieve data as $_POST['check_list'] which will return an array. You can begin from:
echo "<pre>";
print_r($_POST);
echo "</pre>";
EDIT:
The below code will do what you have asked (with doc reference)
<div class="row">
<div class="col-lg-6">
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" name="check_list[]" value="value 1" />
</span>
<input type="text" name="text_value[]" class="form-control" />
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" name="check_list[]" value="value 2" />
</span>
<input type="text" name="text_value[]" class="form-control" />
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" name="check_list[]" value="value 3" />
</span>
<input type="text" name="text_value[]" class="form-control" />
</div>
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" name="check_list[]" value="value 4" />
</span>
<input type="text" name="text_value[]" class="form-control" />
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
For alignment consider the following CSS:
.input-group
{
margin-bottom:10px;
}
You can adjust the size of input area by reducing col-lg-6 to col-lg-4 and so on.
Fiddle Demo : http://jsfiddle.net/27LnT/

How do I use 1 submit button to do 2 actions?

I'm trying to submit data to a list in my mail chimp but also allow the user to upload a file.
Here is my code, the first chunk is the mailchimp autogenerated form which is submitted via:
<div class="clear"><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
and then it's followed by the basic code to submit a file. But at the moment both functions have a submit button. I want only one. Can anyone help?
<body>
<!-- Begin MailChimp Signup Form -->
<div id="mc_embed_signup">
<form action="http://test.us7.list-manage.com/subscribe/post?u=d12b70d4bb1e08c1568d5b392&id=bfb41cbf75" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
<h2>Subscribe to our test mailing list</h2>
<div class="indicates-required">
<span class="asterisk">*</span> indicates required
</div>
<div class="mc-field-group">
<label for="mce-EMAIL">
Email Address <span class="asterisk">*</span>
</label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
<div class="mc-field-group">
<label for="mce-FNAME">First Name </label>
<input type="text" value="" name="FNAME" class="" id="mce-FNAME">
</div>
<div class="mc-field-group">
<label for="mce-LNAME">Last Name </label>
<input type="text" value="" name="LNAME" class="" id="mce-LNAME">
</div>
<div class="mc-field-group input-group">
<strong>Interests </strong>
<ul>
<li><input type="checkbox" value="1" name="group[5597][1]" id="mce-group[5597]-5597-0"><label for="mce-group[5597]-5597-0">Interest 1</label></li>
<li><input type="checkbox" value="2" name="group[5597][2]" id="mce-group[5597]-5597-1"> <label for="mce-group[5597]-5597-1">Interest 2</label></li>
<li><input type="checkbox" value="4" name="group[5597][4]" id="mce-group[5597]-5597-2"><label for="mce-group[5597]-5597-2">Interest 3</label></li>
</ul>
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div>
</div>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
<div class="clear">
<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
</div>
</form>
</form>
</body>
You could accomplish this by routing the MailChimp information through your upload_file.php file.
MailChimp has a great API that you can use for more than just adding a subscriber to a list. You would specifically want the listSubscribe() function. There are some great examples on that page.
Here's more documentation on MailChimp's API: http://apidocs.mailchimp.com/api/2.0/
So in your HTML, you would want to remove the second submit button and make it one form with action="upload_file.php". The PHP file would upload the file, then use the API to submit the data to MailChimp.
You html should look like this:
<body>
<!-- Begin MailChimp Signup Form -->
<div id="mc_embed_signup">
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<h2>Subscribe to our test mailing list</h2>
<div class="indicates-required">
<span class="asterisk">*</span> indicates required
</div>
<div class="mc-field-group">
<label for="mce-EMAIL">
Email Address <span class="asterisk">*</span>
</label>
<input type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
</div>
<div class="mc-field-group">
<label for="mce-FNAME">First Name </label>
<input type="text" value="" name="FNAME" class="" id="mce-FNAME">
</div>
<div class="mc-field-group">
<label for="mce-LNAME">Last Name </label>
<input type="text" value="" name="LNAME" class="" id="mce-LNAME">
</div>
<div class="mc-field-group input-group">
<strong>Interests </strong>
<ul>
<li><input type="checkbox" value="1" name="group[5597][1]" id="mce-group[5597]-5597-0"><label for="mce-group[5597]-5597-0">Interest 1</label></li>
<li><input type="checkbox" value="2" name="group[5597][2]" id="mce-group[5597]-5597-1"> <label for="mce-group[5597]-5597-1">Interest 2</label></li>
<li><input type="checkbox" value="4" name="group[5597][4]" id="mce-group[5597]-5597-2"><label for="mce-group[5597]-5597-2">Interest 3</label></li>
</ul>
</div>
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div>
</div>
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<div class="clear">
<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
</div>
</body>

Categories