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.
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.
I am trying to submit a form using jQuery and ended up hanging the page. I have this one page which have listing and have used table for that. For each listing i am giving edit option. So when user click on edit button on the row, a form will drop below the tr tag and update button. When i click on the update button, page start loading but it keeps loading forever.
This is my form (Sample), I have foreach for listing, so for each row, we have a new form separated with id.
<form action="" class="edit_form 1" method="POST">
<div class="row filter-row">
<div class="col-sm-12 col-md-6">
<div class="form-group">
<label class="focus-label">Order Id</label>
<input type="text" name="order_id" class="form-control floating order_id" value="">
<div class="orderid_error text-danger form_error"></div>
</div>
</div>
</div>
<div class="text-center">
<button class="btn btn-primary submit-btn test_update" data-test_id="1" name="testupdate" type="button" value="test_update">Update</button>
</div>
</form>
JS
$(document).on("click",".test_update",function(){
var test_id = $(this).attr("data-test_id");
if(typeof test_id!="undefined" && test_id!=""){
$(".edit_form."+test_id).submit();
}
});
I think the issue is over here:
$(".edit_form."+test_id).submit();
jQuery searches for the element with class value as => 'edit_form.1' which does not exist in the code.
For this to work you need to have a unique id for every form(assuming you've already done it as you mentioned).
Try this:
$(document).on("click",".test_update",function(){
var test_id = $(this).attr("data-test_id");
if(typeof test_id!="undefined" && test_id!=""){
$(this).parent().parent().submit();
}
});
Since in the DOM nodes every button will be differently saved even though it has same class it shouldn't be a problem adding parent() as it will call the parent of the clicked element but not all the buttons with the class name.
Let me know if it worked. :)
Assigning multiple forms on a single page might be occurring the issue. Since you are using jQuery to submit the form, there is a workaround for the same.
Do not create your form inside the foreach, instead just create one form with empty input box and assign the value through jQuery and then submit the form.
<form id="example" action="" class="edit_form 1" method="POST">
<div class="row filter-row">
<div class="col-sm-12 col-md-6">
<div class="form-group">
<label class="focus-label">Order Id</label>
<input id="orderid" type="text" name="order_id" class="form-control floating order_id" value="">
<div class="orderid_error text-danger form_error"></div>
</div>
</div>
</div>
<div class="text-center">
<button class="btn btn-primary submit-btn test_update" data-test_id="1" name="testupdate" type="button" value="test_update">Update</button>
</div>
JS
$(document).on("click",".test_update",function(){
var test_id = $(this).attr("data-test_id");
if(typeof test_id!="undefined" && test_id!=""){
$("#orderid").val("YOUR VALUE");
$("#example").submit();
}
});
This will definitely work.
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
here is my html
<form name="station" method="post" action="/stations/new" role="form">
<div class="form-group">
<label class="control-label required" for="station_name">Name</label>
<input type="text" id="station_name" name="station[name]" required="required" maxlength="255" class="form-control" >
</div>
<div class="form-group">
<div class="checkbox">
<label for="station_active">
<input type="checkbox" id="station_active" name="station[active]" value="1" />Active</label>
</div>
</div>
<div class="form-group">
<button type="submit" id="station_submit" name="station[submit]" class="btn btn-primary">Ajouter</button>
</div>
<input type="hidden" id="station__token" name="station[_token]" class="form-control" value="aze123aze" >
</form>
i want to get my form using the crawler. I tried the selectButton method like this
$form = $crawler->selectButton('station[submit]')->form(array());
but i get the error : InvalidArgumentException: The current node list is empty.
what is the problem ?
Unfortunately I have no enough rating to just write a comment instead of put it in the answer.
So, could you please show how are you getting $crawler? Problem might be:
$crawler not point to DOM which contains this form
this form appears on page after some java script actions(Ajax for example), but not sure that this is your case.
The selectButton method accept the value The button text. So Try with:
$form = $crawler->selectButton('Ajouter')->form(array());
Hope this help
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.