Insert all documents as per selected departments and so on - mysql

I'm trying to insert Department wise Doc in Docuploadrole table. For first department, all documents which are related to that department will be inserted and then will go for next department to do perform the same until loops ends for all departments.
Below is the screenshot of the input page:
Below is my desired output:
Controller
/*Insert into DB*/
public function DocRoleUploadRoleStore(Request $request)
{
$uploadroledoctype = $request->uploadroledoctype;
$uploadroledept = $request->uploadroledept;
foreach($uploadroledoctype as $docid) {
Docuploadrole::create([
'deptid' => $request->uploadroledept,
'docid' => $docid,
'username' => $request->uploadroleuser,
]);
}
return redirect()->back()->with('status','Upload Role Added successfully');
}
/*Document Type based on Department Dependency*/
public function DocRoleGetUploadRoleDocType(Request $request){
$cid = $request -> post('cid');
$GetUploadRoleDocRole = DB:: table('departments')
->join('doc_types','departments.deptid', '=', 'doc_types.deptid')
->whereIn('departments.deptid',explode(",", $cid))
->get();
$html = ' ';
foreach($GetUploadRoleDocRole as $list)
$html.= '<option value="'.$list ->docid.' '.$list ->deptid.'">'.$list ->deptid.' - '.$list ->docname.'</option>';
echo $html;
}
Blade View
<form action="/document-upload-role-data" method="post">
#csrf
<div class="row">
<div class="col-lg-3">
<select class="form-select form-control" name="uploadroleuser">
<option selected>Select User</option>
#foreach ($usersdata as $item)
<option value="{{$item->username}}">{{$item->username}}</option>
#endforeach
</select>
</div>
<div class="col-lg-3 d-flex">
<select class="form-select form-control" name="uploadroledept[]" multiple id="upload-role-dept">
<option selected>Select Department</option>
#foreach ($departmentDatas as $item)
<option value="{{$item->deptid}}">{{$item->deptname}}</option>
#endforeach
</select>
<input class="mx-1" style="height: 28px;" type="checkbox" name="selectalldepartment" id="upload-role-all-dept"><small style="margin-top: 2px;">All Dept.</small>
</div>
<div class="col-lg-6 d-flex">
<select class="form-select form-control" id="upload-role-doctype" type="checkbox" name="uploadroledoctype[]" multiple>
{{-- <option type="checkbox" selected="false">Document Type</option> --}}
</select>
<input class="mx-1" style="height: 28px;" type="checkbox" name="select_all" id="upload-role-all-doctype-selected"><small style="margin-top: 2px;">All Doc.</small>
</div>
<div class="col-lg-12">
<button type="submit" class="btn btn-primary float-right mt-4">Submit</button>
</div>
</div>
</form>
Route
Route::post('/document-upload-role-data'
[DocumentRoleController::class,'DocRoleUploadRoleStore']);

Related

My Option Value Keeps Resetting After Submit

Hey everyone i have this option menu where you can choose between Day 1, 2 and 3 and once the submit button is clicked it triggers a controller which saves the data to phpmyadmin. The data is being saved okay. But the option menu keeps resetting to day 1 no matter what option is clicked.
<div class="form-group row">
<label class="col-md-4 col-form-label text-md-right" for="choose-day">Choose a Day:</label>
<div class="col-md-6">
<select class="form-control" name="days" id="days-option">
<option class="form-control" value="1">Day 1</option>
<option class="form-control" value="2">Day 2</option>
<option class="form-control" value="3">Day 3</option>
</select>
</div>
Because the form is posted you need to store the value and then add the selected statement to the option that has been chosen. ✌️
Note: your php post script should be running before the if($whatDay == 1) selected else the $whatDay doesn't have a value to use and it doesn't add selected to anything.
<?php
if (isset($_POST['post_Btn'])) {
$whatDay = $_POST['days'];
}
?>
<div class="form-group row">
<label class="col-md-4 col-form-label text-md-right" for="choose-day">Choose a Day:</label>
<div class="col-md-6">
<select class="form-control" name="days" id="days-option">
<option class="form-control" value="1" <?php if($whatDay == 1) { echo 'selected';} ?> >Day 1</option>
<option class="form-control" value="2" <?php if($whatDay == 2) { echo 'selected';} ?> >Day 2</option>
<option class="form-control" value="3" <?php if($whatDay == 3) { echo 'selected';} ?> >Day 3</option>
</select>
</div>

How to give an alert if two selected option values are equal in html jquery?

I am trying to block selecting same input values in html.
I tried lots of jquery functions and including this,
$("#targetLanguage").change(function() {
var a = $("#sourceLanguage").val();
var b = $(this).val();
if(a === b) {
alert(a + ' matches ' + b);
}
});
I think this code should work, however does not do anything. How can I block selecting same input on second select box.
<div class="align-items-baseline d-flex flex-column form-group">
<label for="sourceLanguage"><#spring.message "sourceLanguage"/></label>
<select id="sourceLanguage" name="sourceLanguage" class="form-control" required>
<option value="" selected disabled><#spring.message 'selectSourceLanguage' /></option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectSourceLanguage" />"> ${language.languageCode}</option></#list>
</select>
</div>
<div class="align-items-baseline d-flex flex-column form-group">
<label for="targetLanguage"><#spring.message "targetLanguage"/></label>
<select id="targetLanguage" name="targetLanguage" class="form-control" data-delayy="1000" required>
<option value="" selected disabled><#spring.message 'selectTargetLanguage' </option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectTargetLanguage"/>">${language.languageCode}</option></#list>
</select>
</div>
Try this
html
<div class="align-items-baseline d-flex flex-column form-group">
<label for="sourceLanguage"><#spring.message "sourceLanguage"/></label>
<select id="sourceLanguage" name="sourceLanguage" class="form-control" onchage="check_values()" required>
<option value="" selected disabled><#spring.message 'selectSourceLanguage' /></option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectSourceLanguage" />"> ${language.languageCode}</option></#list>
</select>
</div>
<div class="align-items-baseline d-flex flex-column form-group">
<label for="targetLanguage"><#spring.message "targetLanguage"/></label>
<select id="targetLanguage" name="targetLanguage" class="form-control" data-delayy="1000" onchage="check_values()" required>
<option value="" selected disabled><#spring.message 'selectTargetLanguage' </option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectTargetLanguage"/>">${language.languageCode}</option></#list>
</select>
</div>
Javascript
function check_values()
{
var sourceLanguage = $("#sourceLanguage").val();
var targetLanguage = $("#targetLanguage").val();
if(sourceLanguage == targetLanguage)
{
alert(sourceLanguage + " matches "+ targetLanguage);
}
}
remove the disabled attribute from your options
$("#targetLanguage").change(function() {
var a = $("#sourceLanguage").val();
var b = $(this).val();
if(a === b) {
alert(a + ' matches ' + b);
}
console.log('a = '+ a);
console.log('b = '+ b);
console.log(this);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="align-items-baseline d-flex flex-column form-group">
<label for="sourceLanguage"><#spring.message "sourceLanguage"/></label>
<select id="sourceLanguage" name="sourceLanguage" class="form-control" required>
<option value="" selected ><#spring.message 'selectSourceLanguage' /></option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectSourceLanguage" />"> ${language.languageCode}</option></#list>
</select>
</div>
<div class="align-items-baseline d-flex flex-column form-group">
<label for="targetLanguage"><#spring.message "targetLanguage"/></label>
<select id="targetLanguage" name="targetLanguage" class="form-control" data-delayy="1000" required>
<option value="" selected ><#spring.message 'selectTargetLanguage' </option>
<#list languageList as language>
<option value="${language.getLanguageCode()}" data-error="<#spring.message "selectTargetLanguage"/>">${language.languageCode}</option></#list>
</select>
</div>

jquery datalist show labels

How to show the data list labels but submit the actual value to the database?
<div class="form-group">
<label for="reg_input">Parent Name</label>
<input list="parentName" name="parentsName" id="parentsName" class="form-control">
<datalist style="color: #FFF" id="parentName">
<?php
$pa="select * from parents";
$par=mysql_query($pa);
while ($childrens=mysql_fetch_array($par)) {
?>
<option value="<?php echo $childrens['UserName'];?>" data-value="<?php echo $childrens['UserID'] ?>"></option>
<?php
}
?>
</datalist>
</div>

insert multiple selected value from laravel 5 form

I have a form in laravel 5 as below
#extends('users.home')
#section('content')
<form role="form" action="{{route('postPay')}}" method="post">
<div class="row">
<div class="col-md-12">
<fieldset class="group-border">
<legend class="group-border">Payment Record</legend>
<div class="row">
<div class="col-lg-4">
<div class="form-inline">
<label class="control-label">Current Date:</label>
<input type="text" class="date form-control" id="datepicker" name="date">
</div>
</div>
<div class="col-lg-4">
<div class="form-inline">
<!-- <label class="control-label">Month:</label> -->
<select style="width:250px" multiple="true" class="form-control input-md" id="js-example-basic-single" name="month">
<option value="january">January</option>
<option value="february">February</option>
<option value="march">March</option>
<option value="april">April</option>
<option value="may">May</option>
<option value="june">June</option>
<option value="july">July</option>
<option value="august">August</option>
<option value="september">September</option>
<option value="october">October</option>
<option value="november">November</option>
<option value="december">December</option>
</select>
</div>
</div>
<div class="col-lg-4">
<div class="form-inline">
<label class="control-label">Year:</label>
<input class="form-control input-md" type="text" name="year">
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-lg-1">
<label for="">Customer</label>
</div>
<div class="col-lg-7">
<select class="form-control input-md" name="record_id">
#foreach($records as $record)
<option value="{{$record->id}}">{{$record->user_name}}</option>
#endforeach
</select>
</div>
<div style="float: left;" class="col-lg-4 text-left">
<div class="form-inline">
<label class="control-label">Due: </label>
<input class="form-control input-sm" type="text" name="due">
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-lg-1">
<label class="control-label">Amount:</label>
</div>
<div class="col-lg-7">
<input class="form-control input-sm" type="text" name="amount">
</div>
<div class="col-lg-4">
<div class="form-inline">
<label class="control-label">Advance:</label>
<input class="form-control input-sm" type="text" name="advance">
</div>
</div>
</div>
</fieldset>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-success btn-block">Submit</button>
</div>
<input type="hidden" name="_token" value="{{Session::token()}}"/>
</form>
#endsection
And this is the payment migration table,
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePaymentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->increments('id');
$table->integer('record_id')->unsigned();
$table->date('date');
$table->string('month');
$table->integer('year');
$table->integer('amount');
$table->integer('advance');
$table->integer('due');
$table->dateTime('created_at');
$table->dateTime('updated_at');
$table->foreign('record_id')->references('id')->on('records');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('payments');
}
}
The 'month' field in the form is a multiple selected field made with select2 which I want to store in the database but as far as I know each record in the database does not allow multiple values.How can I make it work? Please,help.
In the multiple select month, use name as array like name="month[]" so you will receive an array with month. Then, you can iterate this array and for each month insert a record in db. You can either create insert query foreach month or use batch insert.
public function store(Request $request)
{
$inputs = $request->all();
$months = $inputs['month'];
//Multiple insert queries
foreach ($months as $month) {
Payments::create([
'date' => $input['date'],
'year' => $inputs['year']
'month' => $month,
'amount' => $inputs['amount'],
'advance' => $inputs['advance'],
'due' => $inputs['due'],
'record_id' => $inputs['record_id']
]);
}
//-------------------------------------------------//
//Batch insert, use either one
$data = [];
foreach ($months as $month) {
$data[] = [
'date' => $input['date'],
'year' => $inputs['year']
'month' => $month,
'amount' => $inputs['amount'],
'advance' => $inputs['advance'],
'due' => $inputs['due'],
'record_id' => $inputs['record_id']
]
}
DB::table('payments')->insert($data);
}
Enjoy coding :)
On submit the form select2 always return the selected options in array. You can convert the array in string using the implode so you can store the result as 'january,may' in month field.

How to disable a input field according to another select field value in bootstrap?

I am using bootstrap, I wanted to know is there something like a feature in bootstrap to do this?
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Flooring material </label>
<div class="col-sm-6">
<select class="form-control" name="flooring_meterial">
<option value="0">-Select-</option>
<option value="1" >Earth,sand</option>
<option value="2">Dung</option>
<option value="3">Wood/planks</option>
<option value="4">Parquet or polished wood</option>
<option value="5">other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="f_m_other">
</div>
</div>
I want to activate this bellow input field, if the above select field value is "other"
Since I could not able to find a short cut for this using bootstrap, I thought to write this in native javascript.
function disable(select_val,input_id) {
var e = document.getElementById(select_val);
var strUser = e.options[e.selectedIndex].value;
if(strUser === "100"){
document.getElementById(input_id).disabled = false;
}
else{
document.getElementById(input_id).value = document.getElementById(input_id).defaultValue;
document.getElementById(input_id).disabled = true;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Principle mode of water supply</label>
<div class="col-sm-6">
<select class="form-control" name="water_supply" id="water_supply" onchange="disable('water_supply', 'w_s_other')">
<option value="0">-Select-</option>
<option value="1">Shared/ public well</option>
<option value="4">Private pipe line</option>
<option value="5">Stream/river</option>
<option value="100" >Other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="w_s_other" id="w_s_other" disabled value="">
</div>
</div>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">x2</label>
<div class="col-sm-6">
<select class="form-control" name="water_supply" id="x2" onchange="disable('x2', 'x2other')">
<option value="0">-Select-</option>
<option value="1">Shared/ public well</option>
<option value="5">Stream/river</option>
<option value="100" >Other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="w_s_other" id="x2other" disabled value="">
</div>
</div>
Try using change event at select element to set input disabled to true if value of select element is "5" with .prop()
$("select").change(function() {
$("+ input", this).prop("disabled", !(this.value === "5"))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<div class="form-group">
<label class="col-sm-6 control-label" for="textinput">Flooring material </label>
<div class="col-sm-6">
<select class="form-control" name="flooring_meterial">
<option value="0">-Select-</option>
<option value="1" >Earth,sand</option>
<option value="2">Dung</option>
<option value="3">Wood/planks</option>
<option value="4">Parquet or polished wood</option>
<option value="5">other</option>
</select>
<input type="text" placeholder="Other" class="form-control" name="f_m_other" disabled="true">
</div>
</div>