How to get input radio name with many different name in laravel - php

In this project, i make something like online test. my question is how to get radio name with many different name in laravel ? i use id to different their name so but i cant get their name in controller.
this is my view
<form action="{{url('kumpulkan')}}" method="POST" class="responsive-form">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#if(Session::get('jurusan') == Multimedia)
<?php
$no = 1;
;?>
#foreach($soal_multimedia as $row)
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="wrapper-soal">
<div class="pertanyaan">
<span class="no-soal"><?php echo $no++ ;?>. </span> <span class="teks-soal">{{$row->soal}}</span>
</div>
<div class="jawaban">
<input type="number" readonly="readonly" name="id_soal" value="{{$row->id}}" hidden="hidden">
<div class="jawaban-item jawaban-a">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_a}}"> {{$row->jawaban_a}}
</div>
<div class="jawaban-item jawaban-b">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_b}}"> {{$row->jawaban_b}}
</div>
<div class="jawaban-item jawaban-c">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_c}}"> {{$row->jawaban_c}}
</div>
<div class="jawaban-item jawaban-d">
<input type="radio" name="jawaban_{{$row->id}}" value="{{$row->jawaban_d}}"> {{$row->jawaban_d}}
</div>
</div>
</div>
</div>
</div>
#endforeach
#endif
<button class="btn btn-default btn-lg">Selesai</button>
</form>
this is my controller
public function Postkumpulkan(Request $request)
{
$insert = array();
$insert['id_soal'] = $request->get('id_soal');
$insert['nama'] = Session::get('nama');
$insert['no_peserta'] = Session::get('no_peserta');
$insert['sekolah'] = Session::get('sekolah');
$insert['jurusan'] = Session::get('');
$insert['jawaban'] = $request->get('jawaban_{{$row->id}}');
}

i think you would better use checkbox and instead of this naming jawaban_{{$row->id}}
use this kind jawaban[{{$row->id}}] in this case you can get all jawabans in one array
like this :
$insert['jawaban'] = $request->get('jawaban');

Since I assume you intend to make your radio buttons to work as a radiogroup (yes, where you can only choose 1) and since that is how radio buttons work, you should just give the same name for all the radio buttons.
<div class="jawaban">
<input type="number" readonly="readonly" name="id_soal" value="{{$row->id}}" hidden="hidden">
<div class="jawaban-item jawaban-a">
<input type="radio" name="jawaban" value="{{$row->jawaban_a}}"> {{$row->jawaban_a}}
</div>
<div class="jawaban-item jawaban-b">
<input type="radio" name="jawaban" value="{{$row->jawaban_b}}"> {{$row->jawaban_b}}
</div>
<div class="jawaban-item jawaban-c">
<input type="radio" name="jawaban" value="{{$row->jawaban_c}}"> {{$row->jawaban_c}}
</div>
<div class="jawaban-item jawaban-d">
<input type="radio" name="jawaban" value="{{$row->jawaban_d}}"> {{$row->jawaban_d}}
</div>
</div>
Then you can just retrieve it in the controller:
$insert['jawaban'] = $request->get('jawaban');
It will retrieve the chosen radio button.

Related

How to store an array as single row for each value in my database

the whole idea is that when a user click on checkbox and save, the form should be submitted with an array containing the checked values, and save them in the database as single row for each value. i don't know if this is possible but i'm sure you guys have a solution or can help me?
and here's my form:
<div class="">
<form style="width: 70%" method="POST" action="{{ URL('/admin/projects/services/save') }}"
enctype="multipart/form-data">
#csrf
<input type="hidden" name="id" id="id" value="{{ #$id }}">
<input type="hidden" name="projectid" id="projectid" value="{{ #$projectid }}">
<div class="row topSpacer">
<div class="col-3">
<p class="bold">Selected Services: <span class="red">*</span></p>
</div>
<div class="col-9">
<input type="text" id="selectedServices" name="selectedServices" value="" readonly>
</div>
</div>
<div class="row topSpacer">
<div class="col-3">
<p class="bold">Select a service: <span class="red">*</span></p>
</div>
<div class="col-9">
#foreach ($services as $service)
<input type="checkbox" name="service" value="<?php echo stripslashes($service->id); ?>">
<label for="service"><?php echo stripslashes($service->name); ?></label>
<br>
<hr>
#endforeach
#error('service')
<span class="text-danger">{{ $message }}</span>
#enderror
</div>
</div>
<div class="row topSpacerHuge">
<div class="col-12 textRight">
<button type="button"
onclick="window.location='{{ url('admin/projects/services/index/'.$projectid) }}'"
class="goBackBtn">Cancel</button>
<input class="saveBtn" id="saveBtn" type="submit" value="Save">
</div>
</div>
</form>
</div>
and my script to get checkboxes value:
<script>
$(function() {
$("input:checkbox[name='service']").click(function() {
$value = [];
$.each($("input:checkbox[name='service']:checked"), function() {
$value.push($(this).val());
$array = $value.join();
});
// console.log(value.join(", "));
$("#selectedServices").val($array);
})
});
</script>
for implementing checked values I recommend to use the select tag
and you can give A name attribute with [] and it will collect all of your data
for example , look at my project's file which I'm trying to collect name of some users :
<select name="users[]" id="example-with-maxHeight" class="multiselect-group" multiple >
#foreach($users as $user)
<option #if($user->average < $averageParticipate )class="alert-danger" #endif value="{{$user->id}}">{{$user->name}} - {{$user->average}}</option>
#endforeach
</select>
have attention to users[] in name attribute
now in your request you can access all of your checked items .
if we look at your code ,can be some thing like this :
<label for="service"><?php echo stripslashes($service->name); ?></label>
<select name="services[]" id="" class="multiselect-group" multiple >
#foreach($users as $user)
<option value = "{{$sevice->id}}"> <?php echo stripslashes($service->name); ?></option>
#endforeach
</select>
now in your controllers you can access them :
request->get('services');
it will give you back an array of selected items .
Notice that id attribute helps me to interact with my java script code
you can rename it to yourselves .

How to validate at least one checkbox is checked in a loop in Laravel form?

I have a collection of checkboxes in my application. What I want to do is validate whether at least one is checked and enable a submit button. Basically a front end validation. According to the current code, the user must select all the checkboxes. I know this should be a small issue but as I am new to Laravel framework it's a little bit confusing for me to understand. Please, someone, help me here.
#foreach ($roles as $role)
<li>
{{ Form::checkbox('roles[]', $role->id, null, ['class="role-li"', 'required']) }}
{{ Form::label($role->name, ucfirst($role->name)) }}
</li>
#endforeach
<form id="testform" action="/comment" method="post" class="m-0 mt-4">
#for($i=0;$i<5;$i++)
<input type="checkbox" name="vehicle1" value="Bike" class="role-li role{{$i}}" onclick='checktest()'> I have a bike<br>
#endfor
</form>
<script>
var minimumonechecked;
var noofboxes = $('.role-li').length;
function checktest(xyz) {
minimumonechecked = false;
for(i=0;i<noofboxes;i++){
if ($('.role' + i).is(':checked')) {
minimumonechecked = true;
}
}
console.log(minimumonechecked)
};
</script>
this code works for me :)
Try this
var checkboxes = $(".check-cls"),
submitButt = $(".submit-btn");
checkboxes.click(function() {
submitButt.attr("disabled", !checkboxes.is(":checked"));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Button should be enabled if at least one checkbox is checked</h1>
<form>
<div>
<input type="checkbox" name="roles[]" id="role-1" class="check-cls"> <label for="role-1">Role 1</label>
</div>
<div>
<input type="checkbox" name="roles[]" id="role-2" class="check-cls"> <label for="role-2">Role 2</label>
</div>
<div>
<input type="checkbox" name="roles[]" id="role-3" class="check-cls"> <label for="role-3">Role 3</label>
</div>
<div>
<input type="checkbox" name="roles[]" id="role-4" class="check-cls"> <label for="role-4">Role 4</label>
</div>
<div>
<input type="checkbox" name="roles[]" id="role-5" class="check-cls"> <label for="role-5">Role 5</label>
</div>
<div>
<input type="submit" class="submit-btn" value="Submit" disabled>
</div>
</form>
jsfiddle

Can I block one table from updating?

I have two tables in my database one practitioner and the second one practitioner_specialty, both of them have fields effective_date and expiry_date. When I use the form to update both if those tables the last effective_date and expiry_date are being saved to both of the tables instead of two separate ones.
Is there a way to block one table from updating so I can update only the second one, or maybe is there a way to save it using different id/name so they will be unique ones for practitioner and practitioner_specialty?
Here are my form and controller used for updating tables.
Controller:
public function updatePractitioner(Request $request, $id)
{
$this->validate($request, [
'effective_date' => 'required',
]
);
$fields = $request->all();
$primary_key = $this->PractitionerRepository->getIdName();
$primary_key_specialty = $this->PractitionerSpecialtyRepository->getIdName();
$practitioner_specialty_id = PractitionerSpecialty::where('practitioner_id', $id)->value('practitioner_specialty_id');
$fields[$primary_key] = $id;
$this->PractitionerRepository->update($fields);
$fields[$primary_key_specialty] = $practitioner_specialty_id;
$this->PractitionerSpecialtyRepository->update($fields);
return back()->with('successUpdate', 'Practitioner has been updated!');
}
update form in blade.php :
<div class="edit-practitioner" style="display: none;">
<form style="box-shadow: none;" action="/practitioner/update/{{$practitioner->practitioner_id}}" method="post"
class="j-pro" id="update-practitioner-form">
{{ csrf_field() }}
<div class="j-content">
<div id="j-row-id" class="j-row">
<div class="row">
<div class="col-sm-12 col-lg-12 col-xl-5">
<div class=" j-unit">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>Practitioner Information</span>
</div>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->effective_date}}"
name="effective_date" id="effective_date">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->expiry_date}}"
name="expiry_date" id="expiry_date">
</div>
<label class="j-label">{{trans('personalData.phoneNumber')}}</label>
<div class="j-input">
</label>
<input type="tel" value="{{$practitioner->phone}}"
name="phone" id="phone">
</div>
<label class="j-label">{{trans('personalData.mobileNumber')}}</label>
<div class="j-input">
<input type="tel" value="{{$practitioner->mobile}}"
name="mobile" id="mobile">
</div>
<label class="j-label">{{trans('personalData.email')}}</label>
<div class="j-input">
<input type="email" value="{{$practitioner->email}}"
name="email" id="email">
</div>
</div>
</div>
<div class="col-xl-1 j-unit"></div>
<div class="col-sm-12 col-lg-12 col-xl-6">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>{{trans('settings.specialty')}}</span>
</div>
<select name="practitioner_specialty_id_update"
id="practitioner_specialty_id_update"
class="form-control-practitioner required">
#foreach($specialties as $specialty)
<option
value="{{$specialty->specialty_id}}">{{$specialty->name}}</option>
#endforeach
</select>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
#isset($practitioner_specialty->practitioner_specialty_id)
<input type="date" value="{{$practitioner_specialty->effective_date}}"
name="effective_date" id="effective_date">
#endisset
#empty($practitioner_specialty->practitioner_specialty_id)
<input type="date" name="effective_date" id="effective_date">
#endempty
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
#isset($practitioner_specialty->practitioner_specialty_id)
<input type="date" value="{{$practitioner_specialty->expiry_date}}"
name="expiry_date" id="expiry_date">
#endisset
#empty($practitioner_specialty->practitioner_specialty_id)
<input type="date" name="expiry_date" id="expiry_date">
#endempty
</div>
</div>
</div>
</div>
<div class="j-divider j-gap-bottom-45 j-gap-top-10"></div>
<button type="submit"
class="btn btn-editpanel btn-success btn-round">Save changes
</button>
<!-- end /.footer -->
<button id="update-cancel-button-practitioner" href="javascript:window.location.href=window.location.href" type="button"
class="btn btn-editpanel btn-danger btn-round">Cancel
</button>
</div>
</form>
</div>
Well you use the same array for both of the tables, hence the reason to update all the fields. You can try to exclude the ones for one form and add them to the other, for example:
$practictionerFields = $request->except('expiry_date', 'effective_date');
$practictionerFields[$primary_key] = $id;
$this->PractitionerRepository->update($practictionerFields);
// and use the other $fields array for the PractitionerSpecialty model.
You can use LOCK query here.
lock is a flag associated with a table. MySQL allows a client session to explicitly acquire a table lock for preventing other sessions from accessing the same table during a specific period. A client session can acquire or release table locks only for itself. It cannot acquire or release table locks for other sessions.
You can read more here: http://mysqltutorial.org/mysql-table-locking

Empty / Null Return Value from Bootstrap Radio Button Group Selection

Would appreciate someone explaining to me why I only get an empty string back from PHP when trying to get radio-button group selection.
$currentlyemployed = null;
if (isset($currentlyemployedyes)) {
$currentlyemployed = "Yes";
}
else {
$currentlyemployed = "No";
}
NOTE: text inputs within the same form post and I can get their values.
Example snippets:
START FORM MARKUP
<div class="row">
<div class="col-sm-6 text-left">
<h5>Are you currently employed?</h5>
<div class="control-group">
<div class="controls">
<label>
<input type="radio" id="currentlyemployedyes" name="currentlyemployed" value="yes" />Yes</label>
<label>
<input type="radio" id="currentlyemployedno" name="currentlyemployed" value="no" />No</label>
<p class="help-block"></p>
</div>
</div>
</div>
</div>
END FORM MARKUP
START PHP CODE SNIPPET
$currentlyemployed = $_POST['currentlyemployed']; RETURNS empty string
/* FYI
$currentlyemployedyes = $_POST['currentlyemployedyes']; RETURNS "Yes"
$currentlyemployedno = $_POST['currentlyemployedno']; RETURNS "No"
*/
END PHP CODE SNIPPET
The code you have written works as expected. You can check out the snippet below.
Bootstrap or not it does not matter. The name="currentlyemployed" sent from the form to php server and then php binds it into $_POST associative global array.
<?php
echo "<pre>";
var_dump($_POST);
echo "</pre>";
echo "Currently Employed: ".$_POST['currentlyemployed'];
?>
<hr>
<div class="row">
<div class="col-sm-6 text-left">
<form method="POST">
<h5>Are you currently employed?</h5>
<div class="control-group">
<div class="controls">
<label>
<input type="radio" id="currentlyemployedyes" name="currentlyemployed" value="yes" />Yes</label>
<label>
<input type="radio" id="currentlyemployedno" name="currentlyemployed" value="no" />No</label>
<p class="help-block"></p>
</div>
</div>
<input type="submit">
</form>
</div>
</div>
I don't consider this a legitimate "answer", but, rather, a "work-around".
Would like to provide attribution, but forget where I found this, might have even been here on stack overflow.
<div class="row">
<div class="col-sm-6 text-left">
<h5>Are you currently employed?</h5>
<div class="control-group">
<div class="controls">
<label>
<input type="radio" id="currentlyemployedyes" name="currentlyemployed" value="yes" **onclick="$('#currentlyemployed').val('Yes');"** />
Yes</label>
<label>
<input type="radio" id="currentlyemployedno" name="currentlyemployed" value="no" **onclick="$('#currentlyemployed').val('No');"** />
No</label>
<p class="help-block"></p>
</div>
**<input name="currentlyemployed" id="currentlyemployed" type="hidden" value="" />**
</div>
</div>
</div>

HTML form not passing checkbox value trough $_POST in php file

I have a form in HTML with some checkboxes and I want to send an email with the selected checkboxes in the body, the problem is that the "text" fields are passing to the PHP variables, but the checkboxes values are always set to uncheck when I test it on the PHP file:
HTML:
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control c-check" >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina
</label>
</div>
PHP:
$reservas_bicicletas = '';
if (isset($_POST['checkbox1citadina'])) {
$reservas_bicicletas .= "Citadina: checked\n";
} else {
$reservas_bicicletas .= "Citadina: unchecked\n";
}
echo $reservas_bicicletas;
$reservas_bicicletas always retrieves the else value.
Need help guys, thanks in advance.
remove c-check
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control " >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina</label>
</div>
Basically i have this (i shorted out the form because there are too many elements):
HTML:
<form name="quick_booking" id="quick_booking" method="post" action="assets/reserva-bicicletas.php">
<div class="col-md-6">
<div class="c-checkbox-list">
<div class="c-checkbox">
<input type="checkbox" name="checkbox1citadina" id="checkbox1citadina" value="checkbox1citadina" class="form-control " >
<label for="checkbox1citadina">
<span></span>
<span class="check"></span>
<span class="box"></span> Citadina</label>
</div>
<button type="submit" class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square">Reservar</button>
</form>

Categories