I have a simple html form and am calling a controller in the routes file, but upon submission I get the following error:
Symfony\Component\Debug\Exception\FatalErrorException thrown with message "Call to undefined method Illuminate\Http\Request::post()"
Stacktrace:
#1 Symfony\Component\Debug\Exception\FatalErrorException in /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:205
#0 Illuminate\Exception\Handler:handleShutdown in <#unknown>:0
On this particular site, I have many forms and even more routes and all work except this one. I've looked again and again and again for some typo or something that might be causing it to fail, but can find nothing.
My form is:
<form class="form-horizontal" action="/warehouse/add_pallet" method="post" accept-charset="utf-8" role="form">
<div class="row">
<div class="col-xs-12">
<div class="pm-well">
<h2>Scan Add Pallet</h2>
<div class="form-group">
<label class="col-xs-12 col-sm-2" for="pallet_location">Location: </label>
<div class="col-xs-12 col-sm-8">
<input type="text" class="form-control" name="pallet_location" id="pallet_location">
</div>
</div>
<div class="form-group">
<label class="col-xs-12 col-sm-2" for="sku">sku: </label>
<div class="col-xs-12 col-sm-8">
<input type="text" class="form-control" name="sku" id="sku">
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<input type="submit" class="pm-btn pm-btn-confirmation medium-btn" value="Add Pallet">
</div>
</div>
</form>
And in my routes file I have:
Route::post('warehouse/add_pallet', 'PalletController#insert');
As a control test, I setup just a straight PHP (no Laravel or any PHP framework) test site and copied the form and changed the form action to simply point to another php script where I could echo out the request method and the post data and that worked as expected.
I'm stumped at this point. Could this be a bug? I have seen lots of other posts about problems with Symfony's Request::[methods] not working.
I am guessing that you have called a method post() inside your controller as either Request::post() or Input::post(). You should use Input::get() instead. The keyword get in the Facade Input is not bound to the $_GET global array. So whether you are submitting data using GET or POST method, you should use Input::get('key') to retrieve them.
However to make sure that the data you are fetching is submitted through the POST method, you can use the Request::method() == 'POST' or Request::isMethod('post') surrounding your code.
Related
I have a blog post that I want to edit in its own view, yet when I put the edits to change, I get a 419 page.
This is my edit view to edit the blog in question specified by its id :
<div id="body" style="color:#333">
<h1 style="color:#333">Update blog</h1>
<form method="POST" action="{{route('blogSingle',$blog->id)}}">
#method('put')
#csrf
<div class="field">
<label for="title" class="label" style='font-size:1.2rem'>Title</label>
<div class="control">
<input type="text" class="input" name="title" id="title" placeholder="Blog Title" value="{{$blog->title}}">
</div>
</div>
<div class="field">
<label for="body" class="label" style='font-size:1.2rem'>Body</label>
<div class="control">
<textarea type="text" class="textarea" name="body" id="body" placeholder="Blog Description">{{$blog->body}}</textarea>
</div>
</div>
<div class="field is-grouped">
<div class="control">
<button type="submit" class="btn btn-secondary">Submit</button>
</div>
</div>
</form>
</div>
I've also a view for every single blog, and in it, I click on a button to redirect to a blog edit view like so :
<div id="body">
{{-- Add a variable blog post here --}}
<h1><span>blog single post</span> <span>Edit Blog</span></h1>
<div>
<img src="images/grew-a-mustache.jpg" alt="Mustache">
<div class="article">
<h2 class="lead">{{$blog->title}}</h2>
<p class="lead">
{{$blog->body}}
</p>
</div>
</div>
</div>
My routes involved are as the following :
Route::get('blog-single/{blog}',[BlogController::class,'show'])->name('blogSingle');
Route::get('blog-single/{blog}/edit', [BlogController::class, 'edit'])->name('blog-edit');
Route::put('blog-single/{blog}',[BlogController::class, 'update']);
Why am I still getting a 419 page expired? I even inspected the page before send and I see my token clearly right here :
A 419 is only thrown when the VerifyCsrfToken middleware fails to validate your token. Having #csrf is not a guarantee that your token won't fail. Reasons your token might fail:
Your session has a different token; check the result of session()->token() and see if it matches with the token in the #csrf field. If it does not, try session()->flush() and re-authenticate if you have to.
You have another form input with _token as name after the #csrf which overwrites the CSRF
You have middleware manipulating the token
You have messed with the vendor file, and so the tokensMatch function isn't functioning as it should (If you think you have done this, you can delete your vendor folder, then run composer clearcache and composer install)
I would like to add, you're not routing to your update method:
<form method="POST" action="{{route('blogSingle',$blog->id)}}">
is calling the show method:
Route::get('blog-single/{blog}', [BlogController::class,'show'])->name('blogSingle');
This does work, since you're specifying the method and the URL is the same, but this is not good practice. It'd be better if you explicitly call the update route (which you haven't even given a name yet). It's also better practice to use the placeholder names instead of letting the route fill the placeholders in order:
Route::put('blog-single/{blog}',[BlogController::class, 'update'])->name('blog-update');
<form method="POST" action="{{route('blog-update', ['blog' => $blog])}}">
You can also use compact to quickly bind the blog to the placeholder.
It's probably something incredibly simple, however I feel like my brain is fried from staring at a computer screen for so long, so any pointers would be appreciated...
I cannot seem to get $_POST to work on my form. When I switch the method to GET - it works fine, but I don't want to have to repeatedly pull form data from the URL as it's quite a lengthy form and would take extra steps etc.
My form data:
<form action="<?= site_url() ?>shop/mock/form/<?= esc($first['gtin']); ?>" name="myForm" method="post">
<div class="p-3 p-lg-5 border">
<div class="form-group row">
<div class="col-md-12">
<label for="brand_name" class="text-black">Brand Name </label>
<input type="text" value="<?= esc($_POST['brand_name']); ?>" class="form-control" id="brand_name" name="brand_name">
</div>
</div>
<?php if (isset($_POST['brand_name'])): ?>
<div class="form-group row">
<div class="col-md-12">
<label for="subbrand_name" class="text-black">Subbrand Name </label>
<input type="email" value="<?= esc($_POST['subbrand_name']); ?>" class="form-control" id="subbrand_name" name="subbrand_name" placeholder="">
</div>
</div>
<?php endif; ?>
</div>
</form>
The only thing I can think of is that I'm sending the form back to the same address everytime, and trying to grab the $_POST data from the latest submission, and repopulate the input values so that the form builds with every submission and reveals the next input etc. var_dump($_POST) shows an empty array every time.
Where am I going wrong?
If you want to receive otp in the form then action empty, if you want to receive from other action, you must put the value into $_SESSION
I'm trying to setup phpunit tests for a project with Laravel 5.1.40 (LTS), php 5.6.28, and phpunit 4.8.27. I'm sorry if this issue has been solved before, but I couldn't find anything.
public function testAdminLogin()
{
$this->visit('/auth/login')
->type('email#address.com', 'email')
->type('1234567890', 'password')
->press('Login');
}
There seem to be an issue with press('STRING') with both <button> and <input> as submit buttons. Below is the error message I receive.
1) ExampleTest::testAdminLogin
A request to [http://localhost/auth/login] failed. Received status code [500].
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:165
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:63
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:85
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:684
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:671
C:\xampp\htdocs\project\tests\ExampleTest.php:52
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
However, when I change the <button> tag to an <a> tag, add an id to it, and replace the press(STRING) function with the click(ID) function, the test passes. I could change the <button> to an <a>, but that would only a temporary fix, and future cases might not allow the tag change.
Below is the HTML form with the <button> tag.
<form action="/auth/login" method="POST" class="form-horizontal">
<div class="form-group">
<label for="email" class="col-sm-4 control-label">E-Mail</label>
<div class="col-sm-6">
<input type="email" name="email" class="form-control" value="{{ old('email') }}" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-4 control-label">Password</label>
<div class="col-sm-6">
<input type="password" name="password" class="form-control" autocomplete="off">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button type="submit" class="btn btn-default btn-login">Login</button>
</div>
</div>
</form>
You said you define auth routes manually. In this case you should have POST route for sending login form:
Route::post('auth/login', ....
It works in a href because it sends GET request for which you have route. Form sends POST request by default.
I'm using Bootstrap with Laravel 4.2. Whenever I try to add more than 1 textarea to my form, I get the following error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'comment'
cannot be null
I have isolated the problem to be that whichever textarea I allow to be first, no matter what variable name I give it or how I try to save it, only the first textarea content gets saved and the others get "null" when I try to read it in using Input::get('my_textarea_name');
Here's a snipped of my form:
{{ Form::open(array('url' => 'course_review', 'id'=>'clearForm'))}}
<div class="row">
<form role="form">
<div class="form-group">
<label for="course_comment">How can students succeed in this course?</label>
<textarea class="form-control" rows="5" id="course_comment" name="course_comment"></textarea>
</div>
</form>
</div>
<hr>
<div class="row">
<form role="form">
<div class="form-group">
<label for="assignments_comment">What kind of assignments and tests did this course have?</label>
<textarea class="form-control" rows="5" id="assignments_comment" name="assignments_comment"></textarea>
</div>
</form>
</div>
<hr>
<div class="row">
<form role="form">
<div class="form-group">
<label for="prerequisites_comment">What skills and prerequisites are required for this course?</label>
<textarea class="form-control" rows="5" id="prerequisites_comment" name="prerequisites_comment"></textarea>
</div>
</form>
</div>
<hr>
<div class="row">
<div class="col-xs-12">
<button type="submit" class="btn btn-info ladda-button" data-style="zoom-out" id="create_or_edit_button"><span class="ladda-label">
<span class="glyphicon glyphicon-plus"></span> Add this review</span>
</button>
</div>
</div>
</div>
{{ Form::close() }}
Here's a snipped from my Model:
$review = new Review();
$review->course_comment = Input::get('course_comment');
$review->assignments_comment = Input::get('assignments_comment');
$review->prerequisites_comment = Input::get('prerequisites_comment');
$review->save();
I was just wondering if anyone is aware of this or knows of a way around it? Thanks in advance.
The problem is you have nested forms.
{{ Form::open(...)}}
<form role="form">
...
</form>
<form role="form">
...
</form>
{{ Form::close() }}
You should get rid of all those <form role="form">...</form>. They are doing nothing and they break HTML standard. Your browser gets confused and only submits one of them at a time. Hence you get null in the not submited ones. You can have several forms in a page but they should not be nested.
From the html5 docs:
4.10.3 The form element
Content model:
Flow content, but with no form element descendants.
you are trying to insert a null value into the comment field of your table.
just make sure it isn't null.
It's pretty obvious that cause of this error is empty value of comment
$review = new Review();
$review->course_comment = Input::get('course_comment' ,false);
$review->assignments_comment = Input::get('assignments_comment', false);
$review->prerequisites_comment = Input::get('prerequisites_comment', false);
$review->save();
So... after a lot of pain, I just figured out the answer to my own question. Hopefully this will help someone else who makes the same mistake. In the code I originally posted, you will see I had wrapped each of my textareas in its own <form role="form"> wrapper:
<div class="row">
<form role="form">
<div class="form-group">
<label for="assignments_comment">What kind of assignments and tests did this course have?</label>
<textarea class="form-control" rows="5" id="assignments_comment" name="assignments_comment"></textarea>
</div>
</form>
</div>
<hr>
This was the source of the problem, as it prevented textareas #2 and #3 from getting read. I'm surprised the first textarea was getting read, in fact. Removing the extra wrappers allowed me to submit more than one textarea successfully. There should be only 1 wrapper for a form. I had copied and pasted this code directly from w3c so it took me a while to realize multiple copies of part of the code would cause me an error.
after many days of trying to get a previously working php form converted to submitting the variables inside a new div I realized that I'm missing something. Other posts show javascript, but Iv'e never used that before and don't understand the need. The new page draws correctly, but the php variables are not being received on the destination page.
HTML for the submit,
<form action="entrance2.php">
<div class="medium-12 columns m-b20"><h4 class="heading">Existing users log-in here :-</h4></div>
</div>
<div class="row">
<div class="user medium-12 columns text-center m-b15"><img src="images/user-img.png" alt=""/></div>
</div>
<div class="row">
<div class="medium-10 columns medium-offset-1"><label for="User Name"></label>
<input id="OwnerEmaili" type="text" placeholder="User Name" name="UserName"></div>
</div>
<div class="row">
<div class="medium-10 columns medium-offset-1"><label for="Password"></label>
<input id="OwnerPasswordi" type="password" placeholder="Password" name="Password"></div>
</div>
<div class="row">
<div class="medium-12 columns text-center"><button class="grd-button">Log In</button></div>
<input type="submit" id="save" name="save" value = "Submit"/>//simple submit for testing
<div class="grd-button1" onClick="document.forms['submit-form'].submit();"></div>
</form></div>
</div>
</div>
Receiving page,
<?php
$p_OwnerEmaili=$_POST["OwnerEmaili"];
$p_OwnerPasswordi=$_POST["OwnerPasswordi"];
echo "$p_OwnerEmaili;$p_OwnerPasswordi";
Only shows the ;.
Is javascript required to submit from inside a div?
You're accessing the wrong items.
You'll need to set your forms input name attributes to this if you want to access them the way you currently have in your php script:
<input id="OwnerEmaili" type="text" placeholder="User Name" name="OwnerEmaili">
And
<input id="OwnerPasswordi" type="password" placeholder="Password" name="OwnerPasswordi">
That will allow you to access them as you do in your PHP script.
You can always check what values have been sent to your php script by using var_dump() or print_r().
<?php print_r($_POST); ?>
Would've shown you that you had UserName & Password set instead of what you wanted.
As Ghost pointed out in the comments, your form will always send user input via GET if you dont specify a method in it. So set this in your form tag:
<form action="entrance2.php" method="post">