How do I save the oldvalue if the input name is variable? - laravel-5

The oldvalue is usually set with the name attribute, in my case the name attribute is variable depending on the id name="option{{$question->id}}", so I can't find the way to save and load the olvalue, this is my code:
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="option{{$question->id}}" id="question" autocomplete="off" value="1" data-oldvalue="{{old('option'.$question->id) }}"> Yes
</label>
<label class="btn btn-primary">
<input type="radio" name="option{{$question->id}}" id="question" autocomplete="off" value="0" data-oldvalue="{{old('option'.$question->id) }}"> No
</label>
</div>
Not sure if the If there is any other approach to get my oldvalues loaded (after validation errors) I would like to know.
Extra info: I'm not sure if the reason the old values don't load is the autocomplete="off". So I would like to know how to alert or get my oldvalue.

Set the values directly and use the default value if no old value is available.
value="{{ old('option' . $question->id, 1) }}"
value="{{ old('option' . $question->id, 0) }}"

Related

How to save multiple inputs to the database when the input is in loop laraval 8

I am working on a Laravel 8 project. I have a row with 3 inputs and a button which can add more line of row. Doesn't matter how many lines I add only the first row is submitted in the database. Please help me with that. Thank you!
index.blade.php:
<h4>Add Products</h4>
<form action="{{ url('insert-products') }}" method="POST" enctype="multipart/form-data">
#csrf
<select class="form-select" name="cateId">
<option value="">Select a Category</option>
#foreach ($category as $item)
<option value="{{ $item->id }}">{{ $item->name }}</option>
#endforeach
</select>
<br>
<label for="">Product Name</label><br>
<input type="text" name="productName" id="quantity2" required><br>
<br>
<input type="checkbox" name="status" required>
<br>
<div class="customer_records">
<input type="text" name="productName" id="quantity_img2" required>
<input type="text" name="productVariant" required>
<input type="text" name="productValue" required>
<a class="extra-fields-customer" href="#">Add More Customer</a>
</div>
<div class="customer_records_dynamic"></div>
<button type="submit" class="btn btn-outline-success waves-effect" id="type-success">Add Product</button>
</form>
controller:
public function insert(Request $request){
$products = new Products();
$products->cateId = $request->input('cateId');
$products->productName = $request->input('productName');
$products->status = $request->input('status') == TRUE ? '1':'0';
$products->productVariant = $request->input('productVariant');
$products->productValue = $request->input('productValue');
if($products->save()){
return redirect('/categories')->with('status',"Products Added Succesfully");
}
else{
return redirect('/categories')->with('status',"Something went wrong");
}
}
You have several transmissions involved:
The request
First of all, you need to make sure that the values are sent at all. Seeing your code, I presuppose that on the request level a single value is being sent. You will need to indicate that there will be multiple values involved, such as
<label for="">Product Name</label><br>
<input type="text" name="productName[]" id="quantity2" required><br>
<br>
<input type="checkbox" name="status[]" required>
<br>
<div class="customer_records">
<input type="text" name="productName[]" id="quantity_img2" required>
<input type="text" name="productVariant[]" required>
<input type="text" name="productValue[]" required>
<a class="extra-fields-customer" href="#">Add More Customer</a>
</div>
Notice the [] indicating multiplicity.
Sidenote: I would strongly discourage you from hard-coding ids into repeated templates, as they will end up being duplicated, which makes your HTML invalid.
The application
While the previous section should fix an issue, it's not guaranteed that it's the only issue. It makes sense to debug and see what you have on application-side sent by the request. If you have all the values, then all is well and good. If not, then investigate the reason and fix it.
The database
You need to make sure that your code attempts to store everything. If so, then a further question is whether it's done successfully. If not, then you will need to debug your code to see what's wrong.

Can I save label title in laravel?

Is there a way to save label title in laravel.
<label for="name">Name</label>
Suppose I want to save "Name" in database.
You can use a hidden field and make its value the same as the label name.
<input name="label" value="Name" type="hidden">
Or you can use something like
<form action="youraction" >
<label for="male">Male</label>
<input type="radio" name="gender" id="male" value="male"><br>
<input type="submit" value="Submit">
</form>
W3Schools - documentation
It will also depend on your use-case may be more information will be helpful.
according to your structure, the label name comes from db, so when you are rendering the blade, you can make a hidden input with value of label name like below:
<form action="{{ route('sampleRoute') }}" >
<label for="{{ $DBVARIABLE }}">{{ $DBVARIABLE }}</label>
<input type="text" name="{{ $DBVARIABLE }}" id="{{ $DBVARIABLE }}" value="{{ $DBVARIABLE }}" style="visibility: hidden;">
<input type="submit" value="Submit">
</form>

Form Fields: Name and Phone Not Passed Along with Request in Paystack

I am trying to implement Paystack in Laravel; I'm using their suggested documentation for Laravel. The issue is that the amount and email get passed onto the Paystack database, but the customer's name and phone details aren't. How do I get it to be passed along to Paystack?
I have configured the checkout process as mentioned in the documentation based on https://github.com/unicodeveloper/laravel-paystack. I'm using Windows 10 and running Laravel 5.6 and PHP 7.3.
<form class="needs-validation" action="{{ route('pay') }}" method="POST">
<div class="row">
<div class="col-md-6 mb-3">
<label for="first_name">First name</label>
<input type="text" class="form-control" id="first_name"
placeholder="first name" value="" name="first_name" required="">
</div>
<div class="col-md-6 mb-3">
<label for="last_name">Last name</label>
<input type="text" class="form-control" id="last_name"
placeholder="last Name" name="last_name" value="" required="">
</div>
</div>
<div class="mb-3">
<label for="email">Email <span class=" text-danger"> * </span>
</label>
<input type="email" class="form-control" id="email" name="email"
placeholder="you#example.com" required>
</div>
{{ csrf_field() }}
<hr class="mb-4">
<button class="btn btn-success btn-lg btn-block" type="submit"
value="Pay Now!">
<i class="fa fa-plus-circle fa-lg"></i> Complete Payment!
</button>
</form>
I expected that after the payment is complete, the customer data should contain the customer email, and name. But it returns only the email in the customer array.
you have to implement it yourself, before the pay button try to have a form like this
<form action="/myform" method="post">
<input type="text" name="fullname">
<input type="number" name="phone_number">
<button>submit</button>
</form>
Route::post('/myform','controllerhere#form1')->name('name');
Route::get('/pay','controller#function')->name('pay');
Route::post('/pay','controller#function')->name('pay');
then your controller to look like this
public function form1(Request $request){
$request->validate([....]);
save the form and return redirect to your payment form like this
..
return redirect('/pay');
}
then payments can be made, please the above codes are just samples to help
Alternatively if you check the https://github.com/unicodeveloper/laravel-paystack package, there is a commented section in the paystack.php; this contains a sample of how to add custom_fields to the metadata.
`<input type="hidden" name="metadata" value="{{ json_encode($array) }}" >`
$array = [ 'custom_fields' => [
['display_name' => "Cart Id", "variable_name" => "cart_id", "value" => "2"],
['display_name' => "Sex", "variable_name" => "sex", "value" => "female"],
]
]
Edit this to add name of the customer and amount then if you dd($paymentdatails) you will see the details in metadata.

Submit checkbox values to Controller

I am having the following form, which I would like to submit to my backend.
<form id="revisionFilter" action="{{ route('revision.filter') }}" method='POST'>
<input class="form-check-input" type="checkbox" value="" id="checkbox1">
<label class="form-check-label" for="checkbox1">
1
</label>
<input class="form-check-input" type="checkbox" value="" id="checkbox0">
<label class="form-check-label" for="checkbox0">
0
</label>
<label class="form-check-label" for="checkboxNull">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="checkboxNull">
Null
</label>
<input type="hidden" name='_method' value='POST'>
<input id="revisionFilterSubmit" type="submit" class='btn btn-danger btn-sm' value='Filter'>
</form>
My backend looks like the following:
routes:
Route::post('/revision/filter', 'RevisionController#filter')->name('revision.filter');
RevisionController:
public function filter(Request $request)
{
Log::info("Request: ");
Log::info($request);
return redirect()->route('revision.rindex');
}
My problem is that when I press the button I am getting redirected to:
The page has expired due to inactivity.
Please refresh and try again.
Instead I would like to see the request with the values of the checkboxes to implement the db saving functionality.
I appreciate your replies!
since I've already made a comment. I will put my answer in the answer box lol.
To answer you, you're not getting any checkbox value since you're checkboxes don't have any name attribute.
You need to put a name attribute to them. E.g.,
<input class="form-check-input" type="checkbox" value="" name="checkbox0" id="checkbox0">
From there, you should now be able to see the value of the checkbox via its name.
Just put same name to your checkbox.For example
<input class="form-check-input" type="checkbox" value="" id="checkbox1" name="revisionFilter">
<input class="form-check-input" type="checkbox" value="" id="checkbox2" name="revisionFilter">

How should I work with checkbox in laravel?

In my create.blade.php I have the following code:
<div class="form-check">
<label for="active">active</label>
<input id="active" name = "active" type="checkbox" class="form-check-input" value="1">
</div>
And in my edit.blade.php the following:
<div class="form-check">
<label for="active">active</label>
<input id="active" name = "active" type="checkbox" class="form-check-input" value="{!!$company->
active!!}">
</div>
SQLFiddle of my db structure with data.
When submitting either with the checkbox checked they work fine
When editing a record with active set to 1, the checkbox isn't checked when opening the form
I know there's no value when not checked and when opening the checkbox isn't 'on' because the value is 1 instead of 'on'.
How can I resolve this problem?
It's actually an HTML problem, you are just using the input the wrong way, you have to use the checked instead of value:
<input
id="active"
name="active"
type="checkbox"
class="form-check-input"
checked="{{ $company->active ? 'checked' : '' }}"
>

Resources