Laravel 5 checkbox not storing value from submit form - laravel

I have added a checkbox to my existing submit form that must change from value 0 to 1 if it is activated. But they dont' save the value, it's always 0.
My code:
<label class="checkbox-inline">
<input type="checkbox" id="inlineCheckbox1" value="true" name="trackingnumber"> You provide an Tracking Number
</label>
ProductController:
if ($request->trackingnumber == 'true') {
$product->trackingnumber = true;
}
What have I forgotten?

Okay i have just change the id="inlineCheckbox1" and works now

In your ProductController do
if ($request->trackingnumber == 'on') {
and set the value also to "on" in your html
and it should work
or you just simply dd($request)

in this situation try dd($request) in your controller and check what it contain in that.
Update:
Hey please put checkbox value="1" and then check what value of checkbox is there in request.

Related

How To Reset File input using livewire

does anybody knows how to reset my input type="file". I'm been using the $this->reset() to clear the property of the input type="file".
$this->reset(['type', 'name', 'institution', 'year', 'certification']);
If you don't need to use id for any other purposes you can render the input like this:
<input type="file" id="{{ rand() }}" >
Instead of using rand() you can also use some kind of counter or any other value that will change on every render. Different id will force livewire to completely replace the element with new one, thus empty. Just setting value="" won't help as input type="file" is immutable. This solution is taken from here
Here is a solution I found in TALL Stack Tips to remove filename from input field after uploading via Livewire.
https://talltips.novate.co.uk/livewire/livewire-file-uploads-using-s3#removing-filename-from-input-field-after-upload
The solution involves implementing a counter to regulate the input file id
To reset a file input in livewire I removed id from that input. And assign a null value to the value.
input without id
<label>Upload Image
<input type="file" wire:model="image" class="w-full">
</label>
to assign null value
$this->image = null;
input create two wire(wire:click and wire:model), It works.
livewire:
<input wire:click="fileClear('fileName')" wire:model="fileName" type="file" id="fileName" name="fileName" >
class:
public function fileClear($inputId)
{
$this->$inputId = "";
}
Clearing #AlexYokisama answer
Yes the easy way is using id as the following.
<input type="file" id="{{ $rand }}" >
And then in you livewire component you need to declare this public $rand
And you can use it safely in your reset function.
public $rand;
public function resetForm()
{
$this->rand++;
}

Retrieve dynamically generate check box array values in controller

in my html i can able to add more fields using jquery and the field am generating is check box array. i need to retrieve this in my controller.
so far i have tried
<input type="checkbox" value="1" name="washing[]" id="washing[]">
<input type="checkbox" value="1" name="dryclean[]" id="dryclean[]">
In controller
$clothtypeid = $request->input('clothtypeid');
$washing = $request->input('washing');
$pressing = $request->input('pressing');
for($i=0;$i<count($clothtypeid);$i++){
//in here how to test if the checkbox is ticked then value=1 else 0 ??
}
I expect if the checkbox is ticked then i have to make 1 else 0
Thanks in Advance
So I did a bit of digging and found this Tutorial which suggests;
Setting the input type to "checkbox" like you did there..
Keep the same name and include the '[]' at the end of the name attribute so that you have the following in your form;
<input type="checkbox" name="check_list[]" value="washing">
<input type="checkbox" name="check_list[]" value="dryclean">
Then in the controller you can get the value by doing the following;
if(!empty($request['check_list'])){
// Loop through array to get the checked values.
foreach($request['check_list'] as $item){
//do something here like;
if($item == 'washing'){ //make db operation or assign to a variable..}
else if($item == 'dryclean'){//make db operation}
}
}
This is a very basic example as you can see and I have to say that I haven't tested it yet but I believe it will work. Try it and give feedback.
EDIT: There is an answer here

ASP.NET MVC 3 Razor view returning multiple values for a Html.CheckBoxFor

I have a View that contains the following Line of code:
//(DaysOfWeek is a bool[])
#Html.CheckBoxFor(m => m.Data.DaysOfWeek[0])
It starts off as false. When the user "checks" the box and returns, it returns a value for both true and false;
Here is what is being passed back as part of the form data
Data.DaysOfWeek[0]:true
Data.DaysOfWeek[0]:false
Why is it doing that?
This is because standard HTML checkboxes return no value if unchecked. To make this annoying behaviour more intuitive, the CheckBoxFor method creates a checkbox and a hidden control with the same name, with a value of false, something like this:
<input type="checkbox" name="myControl" value="True" /> My control
<input type="hidden" name="myControl" value="False" />
What you will see when the form is posted is either:
False // checkbox unchecked
True,False // checkbox was checked
Therefore, to test if the box was checked you should use Contains('True'):
bool checkboxChecked = formCollection["myControl"].Contains("True");

Firefox is caching hidden inputs even with autocomplete turned off

I have a form with the autocomplete attribute set to off.
Inside this form, there is a hidden input element (generated using ASP.NET MVC's Html.HiddenFor() method, but, that should be irrelevant):
<input type="hidden" value="0" name="Status" id="Status" data-val-required="The Status field is required." data-val-number="The field Status must be a number." data-val="true">
When the form is submitted, this value is incremented by one and the model is returned to the view. If I write the status value to the page, I can see that the value was correctly incremented.
However, this hidden input field is always cached. It's never getting the correct value. I tried setting the autocomplete attribute directly on the input element, but without success.
How can I get this hidden field to get the correct value? I'd prefer not to use any Javascript.
Edit: Supplying more code...
Controller
//This code is being executed, and the status is being incremented.
shippingOrder.Status = (shippingOrder.Status != (int)OrderStatus.Closed) ? shippingOrder.Status + 1 : shippingOrder.Status;
View
#using (Html.BeginForm("Number", "Order", FormMethod.Post, new { id = "orderSummary", autocomplete = "off" })) {
...
#Html.HiddenFor(m => m.Status)
}
According to this post here html helpers such as HiddenFor will always first use the posted value and after that the value in the model.
As you said, when writing the value to the page you can see it incremented, yet the helper is using the previously posted value, which is the intended behaviour.
The link does suggest the use of ModelState.Remove("Status") or ModelState.Clear() before assigning the new value.
It also suggest that another option could be not using a HiddenFor helper but instead to build the hidden field yourself. Similar to this:
<input type="hidden" name="Status" value="#Model.Status" />
Either way it looks like your problem is based on similar circumstances.

After button disabled its value did not posted to controller

I have an controller which has check like that
if (form["submit"].ToString() == "Continue")
{
}
and i have button which is doing submit
<button name="submit" value="Continue">Continue</button>
It was all working well until i decided to disable Continue button on submit to prevent double click using this function:
$('form').submit(function () {
if ($(this).valid()) {
$(':submit', this).attr('disabled', 'disabled');
}
});
So now i don't get value form["submit"] posted on controller.
Any thoughts how may i fix that?
I want still prevent second click but be able to get form["submit"] value posted on controller.
Can you control the submit value in a hidden field in the form? I can't tell what other logic you might need, but when the form renders, you could set the hidden field's value to the submit button's value and change it when necessary using the first script below. As long as it has a name attribute and is enabled (which you'd rarely disable a hidden field) then it will post when the form is submitted.
$(function() {
// this assumes your button has id="myButton" attribute
$(':hidden[name="submit"]').val($('#myButton').val());
});
And of course in your form, you would need a hidden field with name="submit"
<input type="hidden" name="submit" value="Continue" />
Then, whenever the state of your form changes, modify the disabled state of the button and the value of the hidden field to reflect the value (if it changed at all).
There are also frameworks you may find useful for UI features like this. KnockoutJS comes to mind. It can be used to "value" bind input elements. It's probably overkill for this small example, but it could be useful if your UI expands. I've added markup, script and comments below if you're interested.
$(function () {
var viewModel = {
submitValue: ko.observable("Continue")
};
ko.applyBindings(viewModel);
$('form').submit(function() {
if($(this).valid()) {
// the following line will change the both the hidden field's value
// as well as the button's value attribute
viewModel.submitValue("some other value");
// I couldn't follow your selector here, but please note I changed
// the name of the submit button in the markup below.
$(':submit, this).attr('disabled', 'disabled');
}
});
});
KnockoutJS requires you use the data-bind attribute to setup your elements. In your case, you'd bind one property to multiple elements like this:
<button name="submitButton" data-bind="value: submitValue"/>Continue</button>
<!-- and bind the same value similarly in the hidden field-->
<input type="hidden" name="submit" data-bind="value: submitValue"/>

Resources