Submit checkbox values to Controller - laravel

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">

Related

Spring accepting data from form getting 404 error

I am trying to take an input from a form made in a jsp called "start.jsp".
<body>
<form action=/addResults"method="post" modelAttribute="results" required="true">
<label for="email">Email Address:</label><br>
<input type="text" id="email" name="email" maxlength="50"required><br>
<label for="fullname">Full Name:</label><br>
<input type="text" id="fullname" name="fullname" maxlength="100"required>
<label for="age">Age (0-120):</label><br>
<input type="text" id="age" name="age" min="0" max="120"required>
<label for="gender">Gender:</label><br>
<input type="text" id="gender" name="gender" maxlength="45"required>
<input type="submit" value="Submit">
</form>
</p>
</body>
I'm then using code in my Controller to add this to a Class. Below is my controller code.
#RequestMapping("/addResults")
public String newHotel(Model model) {
model.addAttribute("results", new TestResults());
return "start";
}
But when I execute my Spring project it gives me an error of 404-not found. I've tried checking and rechecking my links and I can't understand how the links can't find the other pages etc. Any help would very great as I'm quite new to spring and the franework. Fred

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>

Retrieving question from database and display it in radio button

I have a questions table, I managed to create a form and send the data to database, i need a user to answer the question by selecting the best answer from radio button list, i have retrieved the question from a database but the radio button does not work correct, when i select the answer from the first question and the when a go to second question the first question radio button is unchecked, i have attached my code. Please anyone who can help me
my view code
<h2>Welcome to Online Teacher Verification Test:</h2>
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt" value="opt1">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt" value="opt2">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt" value="opt3">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt" value="opt4">{{$qn->opt4}}
</label>
</div>
#endforeach
my controller code
use App\Topics;
use App\Questions;
class QuestionController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$qns= Questions::all();
return view('test.index')->with('qns',$qns);
}
I need when a user answer a question by selecting an answer from button, when he/she moves to the second question the first question should remain checked
Well all the radio buttons have the same name when dom renders. So if you check the first one and then the second, the first one becomes unchecked. Try something like
#foreach($qns as $qn)
{{$qn->question_name}}
<div class="form-check">
<label class="form-check-label" for="opt1">
<input type="radio" class="form-check-input" id="opt1"
name="opt{{ $qn->id }}" value="opt1">{{$qn->opt1}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt2">
<input type="radio" class="form-check-input" id="opt2"
name="opt{{ $qn->id }}" value="opt2">{{$qn->opt2}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt3">
<input type="radio" class="form-check-input" id="opt3"
name="opt{{ $qn->id }}" value="opt3">{{$qn->opt3}}
</label>
</div>
<div class="form-check">
<label class="form-check-label" for="opt4">
<input type="radio" class="form-check-input" id="opt4"
name="opt{{ $qn->id }}" value="opt4">{{$qn->opt4}}
</label>
</div>
#endforeach
This way you will get different name for each questions and you can check for each question. Well now if you want save them to database you can access the answers in controller with different names try like
public function saveAnswers(Request $request)
{
$questions = Questions::all();
foreach($questions as $qns){
$name = "opt".$qns->id;
$answer = $request->$name;
//here you can save or anything you want to do
}
}
Set name of radio button like name="qn{{ $qn->id }}" so that it is unique.

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' : '' }}"
>

Why is PayPal ignoring discount_amount_cart?

I have been using PayPal Payments Standard. Everything has been working great. Now I want to add support for coupon discounts.
According to the documentation, the discount_amount_cart variable appears to be what I want:
Single discount amount charged cart-wide.
It must be less than the selling price of all items combined in the cart. This variable overrides any individual item discount_amount_x values, if present.
Applies only to the Cart Upload command.
So, I add the variable to my form:
<form id="Paypal" name="Paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input id="cmd" name="cmd" type="hidden" value="_cart">
<input id="upload" name="upload" type="hidden" value="1">
<input id="business" name="business" type="hidden" value="kelly#hopskipdesign.com">
<input id="custom" name="custom" type="hidden" value="Client">
<input id="discount_amount_cart" name="discount_amount_cart"
type="hidden" value="$4.00">
<input id="tax_cart" name="tax_cart" type="hidden" value="$1.65">
<input id="currency_code" name="currency_code" type="hidden" value="USD">
<input id="return" name="return" type="hidden" value="http://www.hopskipphotography.com/Order/Confirm/Client">
<input id="cancel_return" name="cancel_return" type="hidden" value="http://www.hopskipphotography.com/Order/Cart/Client">
<input id="cbt" name="cbt" type="hidden" value="Place Order">
<input id="item_name_1" name="item_name_1" type="hidden" value="Photo #6">
<input id="amount_1" name="amount_1" type="hidden" value="$20.00">
<input id="item_number_1" name="item_number_1" type="hidden" value="B49A8382">
<input id="quantity_1" name="quantity_1" type="hidden" value="1">
<input id="item_name_2" name="item_name_2" type="hidden" value="Photo #10">
<input id="amount_2" name="amount_2" type="hidden" value="$20.00">
<input id="item_number_2" name="item_number_2" type="hidden" value="B49A8428">
<input id="quantity_2" name="quantity_2" type="hidden" value="1">
<div class="checkout-button">
<input type="image" src="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif" alt="Check out with PayPal">
</div>
</form>
But when I click the button, everything appears except the discount:
I found a similar question where the answer was to use the tax_cart variable instead of tax_x. But I was already using tax_cart.
Any other ideas as to why discount_amount_cart is not working?
Unlike other amount variables, discount_amount_cart does not like dollar signs.
When I changed the field to:
<input id="discount_amount_cart" name="discount_amount_cart"
type="hidden" value="4.00">
PayPal displayed the discount:

Resources