I’m new to both Laravel and php. Need some help with generating random numbers.
If checkbox 1 (exercise_choice_10) is selected a random number between 1-10 shall be generated.
If checkbox 2 (exercise_choice_20) is selected a random number between 11-20 shall be generated.
If checkbox 3 (exercise_choice_30) is selected a random number between 21-30 shall be generated.
<input type="checkbox" name="exercise_choice_10" value="10" {{ app('request')->input('exercise_choice_10') ? 'checked' : '' }}> Type 1</input></BR>
<input type="checkbox" name="exercise_choice_20" value="20" {{ app('request')->input('exercise_choice_20') ? 'checked' : '' }}> Type 2</input></BR>
<input type="checkbox" name="exercise_choice_30" value="30" {{ app('request')->input('exercise_choice_30') ? 'checked' : '' }}> Type 3</input></BR>
<form action="{{ route('exam.robot', ['course' => $course]) }}" method="post">
#csrf
<input type="checkbox" name="exercise_choice_10" value="10" {{ app('request')->input('exercise_choice_10') ? 'checked' : '' }}> Type 1</input></BR>
<input type="checkbox" name="exercise_choice_20" value="20" {{ app('request')->input('exercise_choice_20') ? 'checked' : '' }}> Type 2</input></BR>
<input type="checkbox" name="exercise_choice_30" value="30" {{ app('request')->input('exercise_choice_30') ? 'checked' : '' }}> Type 3</input></BR>
</BR>
<button class="button">Submit</button>
</BR>
</BR>
{{ $random }}
</form>
In your controller, you can check if the checkbox was checked, and use the value from that checkbox. We know from your examples that the range will be between $value - 9 and $value.
We can use rand($min, $max) to get the random value.
if ($request->has("exercise_choice_10")) {
$value = $request->input("exercise_choice_10");
$randomValue = rand($value-9, $value);
}
Repeat the same for your other checkboxes and return the $randomValue to your view.
Keep in mind, by using checkboxes, all 3 can be checked at any given time - so you may want radio-buttons instead (where only one per name can be checked).
Thank you for your reply Qirel :)
This solution (see below) did only work with radiobuttons. When using checkbox the last checked checkbox will override the rest. So Im still working on a solution for checkbox.
if ($request->input('exercise_choice_10')) {
$value = $request->input("exercise_choice_10");
$random = rand($value-9, $value);
}
else {
}
if ($request->input('exercise_choice_20')) {
$value = $request->input("exercise_choice_20");
$random = rand($value-9, $value);
}
else {
}
if ($request->input('exercise_choice_30')) {
$value = $request->input("exercise_choice_30");
$random = rand($value-9, $value);
}
else {
}
Related
I have been in little weird situation. I am passing value as 0 when checkbox is checked in a particular scenario. But, during update or during any error scenario, the value returned be 0 and by default with value 0 checkbox is not being checked.
<input type="checkbox" {{ old('day_0', $data->day_0 == 0) ? 'checked' : '' }} class="onoffswitch-checkbox form-control #error('day_0') is-invalid #enderror" id="day_0" name="day_0" value="0">
I know this might be little weird to pass value as 0 but is there any way to tackle with this by passing value 0
Your parenthesis for the old() function doesn't match up to be valuables, with your current code, the second argument becomes an expression $data->day_0 == 0 - meaning you end up getting old('day_0', true) == 0 if you use the fallback value and that value was 0.
The function old() will use the old value if it existed, otherwise $data->day_0 as a default value if not.
<input type="checkbox"
{{ old('day_0', $data->day_0) == 0 ? 'checked' : '' }}
class="onoffswitch-checkbox form-control #error('day_0') is-invalid #enderror"
id="day_0"
name="day_0"
value="0">
just use like shown below
<input type="checkbox" {{ old('day_0', $data->day_0) === 0 ? 'checked' : '' }}
class="onoffswitch-checkbox form-control #error('day_0') is-invalid #enderror"
id="day_0" name="day_0" value="0">
i'm creating admin of my articles, in my resource EDIT post i have some problems, i would like show all tags of my post that i'm editing, i'm tryng like this:
I created a variable array with all tags id relation with the post edited:
$post_tags[0]->tag_id // it return some id of tag's post
Now i would show all tag's post like input checked in my form edit.blade.php
#for ($i = 0; $i < count($all_tags); $i++)
#if($all_tags[$i]->id != $post_tags[$i]->tag_id)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$all_tags[$i]->slug}}" value="{{$all_tags[$i]->id}}" data-parsley-mincheck="2" class="flat"/>
#else
<input style="cursor: pointer;" type="checkbox" name="tags[]" id="{{$all_tags[$i]->slug}}" value="{{$all_tags[$i]->id}}" data-parsley-mincheck="2" class="flat" checked/>
#endif
#endfor
But i'm getting this error:
ErrorException in Collection.php line 1187: Undefined offset: 5 (View:
C:\xampp\htdocs\sofis\resources\views\admin\post\edit.blade.php)
Larvael 5.3 and above
As others have said, you need to check to make sure the index exists. You can also simplify your logic some by using #foreach instead of #for:
#foreach ($all_tags as $tag)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$tag->slug}}" value="{{$tag->id}}" data-parsley-mincheck="2" class="flat" {{ (isset($post_tags[$loop->index]) && $post_tags[$loop->index]->tag_id == $tag->id) ? 'checked' : '' }} />
#endforeach
You could improve Samsquanch's answer to match with versions lesser than 5.3. You can access array index by defining $key in foreach() like shown bellow.
#foreach ($all_tags as $key => $tag)
<input style="cursor: pointer;" type="checkbox" name="tags[]" id=" {{$tag->slug}}" value="{{$tag->id}}" data-parsley-mincheck="2" class="flat" {{ (isset($post_tags[$key]) && $post_tags[$key]->tag_id == $tag->id) ? 'checked' : '' }} />
#endforeach
This means $post_tags[$i] doesn't have element with index = 5. You shouldn't use $array[$index] method if you're not sure element with this index exists. Or you should check it before use:
#if (isset($post_tags[$i]) && $all_tags[$i]->id != $post_tags[$i]->tag_id)
I guess $post_tags does not have index 5, so $post_tags[$i] fails
I have a basic UI where users can add a simple list with a label and a value. I want to loop through that list to store the data in a "Detail" model.
I have the following code.
Controller:
$details = $request->input('detail_label');
foreach($details as $key => $value)
{
if(!empty($request->input('detail_value.'.$key))) {
// if the detail has an existing ID
if($request->input('detail_id.'.$key)) {
$detail = Detail::find($request->input('detail_id.'.$key));
} else {
$detail = new Detail;
}
$detail->type = $request->input('detail_type.'.$key);
$detail->label = $request->input('detail_label.'.$key);
$detail->value = $request->input('detail_value.'.$key);
if($request->input('detail_privacy.'.$key) == 1) {
$detail->privacy = 1;
} else {
$detail->privacy = 0;
}
$user->details()->save($detail);
}
}
View:
#foreach($user->details as $detail)
<div class="detail">
<input type="hidden" name="detail_id[]" value="{{ $detail->id }}">
<label>Type
<select name="detail_type[]">
<option #if($detail->type == '1')selected #endif value="1">Phone</option>
<option #if($detail->type == '2')selected #endif value="2">Phone (mobile)</option>
<option #if($detail->type == '3')selected #endif value="3">URL</option>
<option #if($detail->type == '4')selected #endif value="4">Email</option>
</select>
</label>
<label>Label
<input type="text" name="detail_label[]" value="{{ $detail->label }}">
</label>
<label>Value
<input type="text" name="detail_value[]" value="{{ $detail->value }}">
</label>
<label>Private?
<input type="checkbox" name="detail_privacy[]" #if($detail->privacy == true) checked #endif value="1">
</label>
<label>Delete?
<input type="checkbox" name="detail_delete[]" value="{{ $detail->id }}">
</label>
</div><!-- / detail -->
#endforeach
Every aspect of my code works as I had planned except the detail_privacy field. It sets and unsets the boolean privacy attribute but it pays no attention to which $key I want. It always sets it according to the order in the loop. If I just set one detail to be private it will be first. If I set two, (whichever two), it will be the first and second.
Something is clearly wrong with my logic but I can't tell what.
Any help would be really appreciated. Thanks!
The reason it doesn't work is that non-checked checkboxes are not included in the post data. You may see it by dumping value of $request->input('detail_privacy').
To be able both to edit existing and add new details, you need to set keys on inputs in the form. Anything really, as long as you keep track of it. To make things easier you can add hidden input named same as the checkbox, so detail_privacy will be always present in the post data. For example:
#foreach ($user->details as $key => $detail)
<input type="hidden" name="detail_id[{{ $key }}]" value="{{ $detail->id }}">
...
<input type="hidden" name="detail_privacy[{{ $key }}]" value="0">
<input type="checkbox" name="detail_privacy[{{ $key }}]" #if($detail->privacy == true) checked #endif value="1">
...
#endforeach
If you add new fields dynamically ie. with javascript you need to respect those keys. Its quite simple though, just pass last value of $key to your js and you're set.
Also I would recommend different notation for input names: details[{{ $key }}][id] instead of detail_id[{{ $key }}]. This way controller action will become much simpler:
foreach ($details as $detail) {
if (! empty($detail['id'])) {
...
}
}
If the Boolean values are stored as TINYINTS in the Database (as in: 0 or 1); then perhaps taking out the Boolean true || false may resolve the Issue. Not Certain but guessing it's worth the try:
<!-- JUST TRY IT LIKE SO: #if($detail->privacy) checked #endif -->
<label>Private?
<input type="checkbox"
name="detail_privacy[]"
#if($detail->privacy) checked #endif value="1"
>
</label>
Or Hypothetically:
<!-- YOU MAY ALSO USE "1" LIKE SO: #if($detail->privacy==1) checked #endif -->
<label>Private?
<input type="checkbox"
name="detail_privacy[]"
#if($detail->privacy==1) checked #endif value="1"
>
</label>
I have created a contact form on my website using php and some of the values, more specifically the ones that have a checkbox-type input, are not send to my DB. I think there must be something wrong with the way I use the checkboxes or something. Upon submitting a form I get several notices like such:
Notice: Undefined index: ironing in
/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 79
Notice: Undefined variable: POST in
/Applications/XAMPP/xamppfiles/htdocs/wordpress/demo.php on line 94
The rest of the code seems to work fine and the data is loaded into my DB.
Here is some of my code in the demo-form.php file that I think is wrong:
<h4>i) Cleaningservice:</h4>
<p>Experience (in years): <input type="number" name="cleanExpInYears"/> </p>
<p>Skills: <br>
<input type="checkbox" name="basic" value="yes"> Basic cleaning<br>
<input type="checkbox" name="kitchen" value="yes"> Kitchen tools<br>
<input type="checkbox" name="laundry" value="yes"> Laundry<br>
<input type="checkbox" name="ironing" value="yes"> Ironing<br>
<input type="checkbox" name="window" value="yes"> Windows<br>
<p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
</p>
And here is the corresponding demo.php file that connects to the DB and stores the data there:
/* Cleaningservice data is gathered... */
$clean_value1 = $_POST['cleanExpInYears'];
$clean_value2 = $_POST['basic'];
$clean_value3 = $_POST['kitchen'];
$clean_value4 = $_POST['laundry'];
$clean_value5 = $_POST['ironing'];
$clean_value6 = $_POST['window'];
$clean_value7 = $_POST['other_clean'];
/* ...and inserted into the table cleaningservice */
$sql = "INSERT INTO cleaningservice (applicant_id, cleanExpInYears, basic, kitchen, laundry, ironing, window, other)
VALUES (LAST_INSERT_ID(),'$clean_value1', '$clean_value2', '$clean_value3', '$clean_value4', '$clean_value5', '$clean_value6', '$clean_value7')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
What am I missing here ??
Thanks in advance
The browser only passes the checkbox to the PHP script if the checkbox was actually checked.
So in PHP you have to check that the $_POST variable actually exists before using its contents, and provide the UNCHECKED i.e. default option if it does not exist.
/* Cleaningservice data is gathered... */
$clean_value1 = $_POST['cleanExpInYears']);
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = $_POST['other_clean'];
To avoid error messages like `array index does not exist you should check all POST/GET values exist like this anyway.
/* Cleaningservice data is gathered... */
$clean_value1 = isset($_POST['cleanExpInYears']) ? $_POST['cleanExpInYears'] : '';
$clean_value2 = isset($_POST['basic']) ? $_POST['basic'] : 'No';
$clean_value3 = isset($_POST['kitchen']) ? $_POST['kitchen'] : 'No';
$clean_value4 = isset($_POST['laundry']) ? $_POST['laundry'] : 'No';
$clean_value5 = isset($_POST['ironing']) ? $_POST['ironing'] : 'No';
$clean_value6 = isset($_POST['window']) ? $_POST['window'] : 'No';
$clean_value7 = isset($_POST['other_clean']) ? $_POST['other_clean'] : '';
You must switch between name and values
<p>Skills: <br>
<input type="checkbox" value="basic" name="yes"> Basic cleaning<br>
<input type="checkbox" value="kitchen" name="yes"> Kitchen tools<br>
<input type="checkbox" value="laundry" name="yes"> Laundry<br>
<input type="checkbox" value="ironing" name="yes"> Ironing<br>
<input type="checkbox" value="window" name="yes"> Windows<br>
<p> Please tell us about other skills: <input type="text" name="other_clean"/> </p>
</p>
and in PHP side, you will find your selected values in var_dump($_POST['yes'])
The following is my laravel blade code. I want automatically check the radio with lowest value.
{
#foreach($service_stages as $ss)
<input class="" id="money" name="money" type="radio" id="" value="{{$ss->money}}" onclick="generateDate()" checked>{{$ss->money}}
#endforeach
}
First write some code to find the smallest value (e.g. in your controller). Then save it to a variable which you provide to your blade view. Lets assume you have it in the variable $smallestValue then you can try the following:
#foreach($service_stages as $ss)
{{ Form::radio('money', $ss->money, ($ss->money == $smallestValue) ? true : false , ['id' => 'money', 'onclick' => 'generateDate()']) }}
#endforeach
This will set the checked="checked" if $ss->money == $smallestValue.