Old Data for Check Box - laravel-5

How Can I get old value for the below check boxes
#if (count($chests) > 0)
#foreach ($chests as $chest)
<br> Day:
#for ($i = 0; $i < $days; $i++)
<label><input type="checkbox" name="{{str_replace(' ', '_', $chest->name)}}_days[]"
value="{{$i+1}}">{{$i+1}}</label>
#endfor
<br>
<label><input type="checkbox" name="chest[]" value="{{$chest->name}}">{{$chest->name}}</label><br>
<div id="div01">
<input type="text" id ="hint" name="{{str_replace(' ', '_', $chest->name)}}_hints[]" placeholder="Hints / Repeats etc">
</div>
#endforeach
#else
No Register Machines
#endif
How I get the old value of checkbox

Related

storing array in database when checkbox is not checked

Im using Laravel 7 and trying to save opening hours to database.
Im using checkbox and time fields for each day of week. If all the days are selected, it stores to database with no problem. If the day is not checked It doesn't store the dayi. i need it to be saved inactive.
here is my view and controller files. What am i doing wrong here??
<td>
<div class=" weekDays-selector">
<input type="checkbox" id="day-mon" class="weekday" name="day[]" value="1" #if($days['0']->status == 'active') checked #endif />
<label for="day-mon">Monday </label>
</div>
</td>
<td>
<div class="input-group clockpicker" data-autoclose="true" #if($days['0']->status == 'inactive') style="display: none" #endif id="mon-from">
<span class="input-group-addon">
<span class="fa fa-clock-o"></span>
</span><input type="text" class="form-control" placeholder="09:00" name="from[]" autocomplete="off" value="{{\Carbon\Carbon::createFromFormat('H:i:s',$days['0']->from)->format('G:i')}}">
</div>
</td>
Controller
$from = $request->from;
$to = $request->to;
$day = $request->day;
for( $count = 0; $count < 7; $count++ )
{
$newday = new Day;
$newday->vendor_id = $setting->id;
$newday->day = $count+1;
$newday->from = $request->day[$count] ? $from[$count] : '00:00:00';
$newday->to = $request->day[$count] ? $to[$count] : '00:00:00';
$newday->status = $request->day[$count] ? 'active' : 'inactive';
$newday->save();

Laravel : save radio group with difference key

I want to save radios group in database with different question_id
I tried to use form with radio's input have name=answer
controller :
public function store(Request $request, Survey $survey)
{
$question = \App\Question::where('survey_id', '=' , $survey->id)->get();
$request->validate([
'answer'=>'required'
]);
$arr = $request->except('_token');
$newAnswer = new Answer();
$newAnswer->answer = request()->get('answer');
foreach ($survey->questions as $key=>$question){
$newAnswer->question_id = $question->id;
}
$newAnswer->user_id = Auth::id();
$newAnswer->last_ip = request()->ip();
$newAnswer->survey_id = $survey->id;
$newAnswer->commentaire = request()->get('commentaire');
givePoint(new VoteAdded($newAnswer));
$newAnswer->save();
return redirect()->action('SurveyController#view_survey_answers', [$survey->id]);
}
view :
<div id="Create" class="col-12" style="display:none;">
{!! Form::open(array('action'=>array('AnswerController#store', $survey->id))) !!}
<div class="text-center">
<img class="rounded"
src="https://interactive-examples.mdn.mozilla.net/media/examples/grapefruit-slice-332-332.jpg"
alt="Grapefruit slice atop a pile of other slices" />
</div>
<hr>
#foreach ($survey->questions as $question)
<p class="flow-text">Question {{ $zero++}} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="form-group" style="margin-left: 20px;">
<div class="input-field col s12">
<input id="answer" type="text" name="answer">
<label for="answer">Answer</label>
</div>
</div>
#elseif($question->question_type === 'textarea')
<div class="form-group" style="margin-left: 20px;">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
#if($value === 'else')
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{$value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
<div id="textboxes" style="display: none">
<br>
<textarea class="form-control" name="commentaire" id="exampleFormControlTextarea1" rows="3" placeholder="Write a large text here ..."></textarea>
</div>
</div>
#else
<p style="margin:0px; padding:0px;">
<div class="form-group" style="margin-left: 20px;">
<input name="answer[{{$question->id}}]" class="custom-control-input" type="radio" id="{{ $value }}" value="{{ $value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
</div>
</p>
#endif
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<div class="form-group">
<input type="checkbox" id="{{ $value }}" name="answer" value="{{$value}}"/>
<label for="{{$value}}">{{ $value }}</label>
</div>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#endforeach
<div class="form-group">
{{ Form::submit('Submit Survey', array('class'=>'btn btn-success mt-4')) }}
</div>
{!! Form::close() !!}
</div>
Tables :
i have already made it :
Answer table = id, survey_id , question_id , answer . commentaire ,
timestamp
Survey table : id title ...
question table : id , suvey_id , option_type , options
I got in my database a array: ["question_id" = "value"]
what a want is for example i have 2 questions , in my database i want to save 2 row with some survey id but different question id
could you help me to do that ?
-Since 1 survey can have many questions create a separate table for questions like survey_questions
-You can insert in multiple tables at the same time by writing insert queries for the survey and survey questions table separately and wrapping it in a transaction. In Laravel, you can do something like
try{
DB::transaction();
//insert in survey table
//insert in survey questions table
DB::commit();
} catch(\Exception $e){
DB::rollback()
}

Save options radios or checkbox

I want to save radio from form in database but I get "on" in the database, I don't know why.
I tried to use HTML form with labels, radios and checkbox but I see in the database saving "on".
Controller :
public function store(Request $request, Survey $survey)
{
// remove the token
$arr = $request->except('_token');
foreach ($arr as $key => $value) {
$newAnswer = new Answer();
if (! is_array( $value )) {
$newValue = $value['answer'];
} else {
$newValue = json_encode($value['answer']);
}
$newAnswer->answer = $newValue;
$newAnswer->question_id = $key;
$newAnswer->user_id = Auth::id();
$newAnswer->survey_id = $survey->id;
$newAnswer->save();
$answerArray[] = $newAnswer;
};
return redirect()->action('SurveyController#view_survey_answers', [$survey->id]);
}
view :
{!! Form::open(array('action'=>array('AnswerController#store', $survey->id))) !!}
#forelse ($survey->questions as $key=>$question)
<p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="input-field col s12">
<input id="answer" type="text" name="{{ $question->id }}[answer]">
<label for="answer">Answer</label>
</div>
#elseif($question->question_type === 'textarea')
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input name="{{ $question->id }}[answer]" type="radio" id="{{ $key }}" />
<label for="{{ $key }}">{{ $value }}</label>
</p>
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input type="checkbox" id="something{{ $key }}" name="{{ $question->id }}[answer]" />
<label for="something{{$key}}">{{ $value }}</label>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#empty
<span class='flow-text center-align'>Nothing to show</span>
#endforelse
{{ Form::submit('Submit Survey', array('class'=>'btn waves-effect waves-light')) }}
{!! Form::close() !!}
You forgot to give a value attribute to your checkbox and radio inputs, for example something like this:
<input name="{{ $question->id }}[answer]" type="radio" id="{{ $key }}" value="{{ $key}}"/>
<input type="checkbox" id="something{{ $key }}" name="{{ $question->id }}[answer]" value="{{ $key}}"/>

How to get the data after submit the form based on the form input value using laravel

My controller contain single function with multiple API calling when I returned the controller function to my view api data displayed automatically but i need to display the data while submit the form. Please suggest any solution.
My Controller Code
public function domaincheck(Request $request)
{
ini_set('max_execution_time', 300);
<!-- Domain Availability Check -->
$sld = $request['sld'];
$tld = $request['tld'];
$response = file_get_contents('https://resellertest.enom.com/interface.asp?command=check&sld='. $sld .'&tld='. $tld .'&uid=resellid&pw=resellpw&responsetype=xml');
$data = simplexml_load_string($response);
$configdata = json_encode($data);
$final_data = json_decode($configdata,true);
<!--Domain Name Suggestions -->
$response1 = file_get_contents('http://resellertest.enom.com/interface.asp?command=GETNAMESUGGESTIONS&uid=resellid&pw=resellpw&SearchTerm='. $sld .'&MaxResults=5&ResponseType=XML');
$data1 = simplexml_load_string($response1);
$configdata1 = json_encode($data1);
$final_data1 = json_decode($configdata1,true);
}
My view Code
<form class="form-horizontal" method="get">
<div class="form-group">
<div class=" col-lg-2"></div>
<div class="col-lg-8">
<div class="input-group m-b">
<span class="input-group-addon" style="padding-left:10px; background-color: #999;" class='unclickable'>www</span>
<input type="text" name="sld" class="form-control">
<span class="input-group-addon">
<select class="form-control" name="tld" style="width: 100px;">
<?php $j = 0; ?>
#foreach($final_data2['tldlist']['tld'] as $value)
<?php $j++; ?>
#endforeach
#for ($i = 0; $i < $j-1;)
<option value="{{($final_data2['tldlist']['tld'][$i]['tld'])}}">{{($final_data2['tldlist']['tld'][$i]['tld'])}}</option>
<?php $i++; ?>
#endfor
</select>
</span>
<span class="input-group-addon">
<button type="submit" class="btn btn-sm btn-success">Submit</button>
</span>
</div>
<p class="text-center text-dark customFont" >
#foreach($final_data as $key=>$value)
#if($key=='DomainName')
<b>{{$value}}</b> <b>-</b>
#endif
#if($key=='RRPText')
<b>{{$value}}</b>
#endif
#endforeach
</p>
{{print_r($final_data1)}}
When submit the form (Response) it shows the domain name is available or not available. Response1 is shows the domain name suggestions but it shows the data with out submit the form.Please suggest any solutions

Multiple Checkbox Saving to DB 1 or 0

Creating multiple question checkboxes and I need to send two possible cases 1 or 0 to the database. However, it always sends the last one I click as 1 all the others 0. So for example, if a user clicks/checks the first one and the last I want to send to the database both 1 and the other two unchecked 0.
Controller
<?php
public function create($request)
{
foreach ($request['options'] as $key => $option) {
Answer::create([
'question_id' => $this->get(),
'text' => $option,
'correct' => $request['correct'] == $key ? 1 : 0
]);
}
}
View
#for($i = 1; $i<=4; $i++)
<div class="form-group {{ $errors->has('options.'.$i) ? ' has-error': '' }}" id="option{{ $i }}">
<div class="checkbox col-xs-2 control-label" style="margin-top: -2px">
<label>
<input id="cc" type="checkbox" name="correct" value="{{$i}}" {{ $i==1 ? 'checked' : '' }} >
<!--
{!! Form::hidden('correct',0) !!}
{!! Form::checkbox('correct',1,false) !!}
-->
</label>
</div>
<div class="col-xs-8">
<input type="text" name="options[{{ $i }}]" value="{{ old('options.'.$i) }}"
class="form-control" placeholder="#lang('general.option') {{ $i }}">
#if($errors->has('options.'.$i))
<div class="col-xs-12"></div>
<span class="help-block">
<strong>{{ $errors->first('options.'.$i) }}</strong>
</span>
#endif
</div>
</div>
#endfor
Either mention array of control for checkbox as below
<input id="cc" type="checkbox" name="correct[]" value="{{$i}}" {{ $i==1 ? 'checked' : '' }} >
change name="correct" to name="correct[]"
OR
give different name to each checkbox using incremental variable like
name="correct" to name="correct_".$i

Resources