Checkbox data isn't being stored to DB - php

I'm having a problem not being able to upload answers in checkbox to DB.
I have a checkbox in my form that a user can check multiple boxes or maybe choose one or none. I am trying to using implode method to upload the answers to DB and this is what my code looks now.
ProductController.php
public function store(ProductRequest $request)
{
$product = new Product;
$product->user_id = $request->user()->id;
$product->name = $request->name;
if($request->has('allergy[]')) {
$allergy = $request->allergy;
$string = implode(",", $allergy);
$product->allergy = $string;
}
$product->save();
return redirect('product/create')->with('message', 'Your answer has been uploaded.');
}
register.blade.php
<form action="{{ url('/product') }}" method="post" enctype="multipart/form-data" class="form-group">
{{ csrf_field() }}
<p><label>menu<br>
<input class="form-control" type="text" name="name" value="{{ old('name') }}">
</label></p>
<p><label>allergy<br>
<input type="checkbox" name="allergy[]" value="1"> apple
<input type="checkbox" name="allergy[]" value="2"> orange
<input type="checkbox" name="allergy[]" value="3"> banana
</label></p>
<p><button type="submit" class="btn btn-success">register</button></p>
</form>
No error is showing after pressing register button but there's no data stored in DB when I check it. "allergy" column in DB is like charset(255) utf8mb4_unicode_ci NULLABLE.
I appreciate if you could tell me what I'm doing wrong or what I should do. Any help would be appreciated as I have tried multiple methods with no success.

You don't need to check if the value of allergy has a string representation of an array, as the value will be presented to you in array format if it was sent using array notation. Due to this, your if check is failing:
if ($request->has('allergy')) {
$product->allergy = implode(',' $request->get('allergy'));
}
Will correctly set the value of $product->allergy to the comma separated string you're looking for.

Related

Laravel Blade some values in 2 dimensional input array missing

i have a form that has editable answers, which are built like the following:
#foreach ($question->answers as $answer)
<input type="text" name="answers[{{$loop->index}}][text]">
<input type="hidden" name="answers[{{$loop->index}}][id]" value="{{$answer->id}}">
#endforeach
Which generates the html
<input type="text" name="answers[0][text]" value="Somevalue">
<input type="hidden" name="answers[0][id]" value="1">
<input type="text" name="answers[1][text]" value="Somevalue">
<input type="hidden" name="answers[1][id]" value="2">
In my Controller, I try to get the values from the request like this:
$requestAnswers = $request->input('answers');
foreach($requestAnswers as $key => $value) {
$id = $value['id'];
$text = $value['text'];
// Some answer handling
}
Somehow, when I try to access my answers, even when I dd($request->input('answers')) only some values show up, sometimes not having a text, sometimes not appearing at all.
Is there an error in my code or is the problem related to something else?
Thanks in advance!

Multiple file upload with optional items

I have a form with multiple inputs like this:
<div id="users">
<input type="text" name="title[]" required>
<input type="file" name="images[]">
<input type="text" name="title[]" required>
<input type="file" name="images[]">
<input type="text" name="title[]" required>
<input type="file" name="images[]">
</div>
<input type="button" value="add new user">
<input type="submit" value="submit">
the problem is that selecting image is optional and if the user doesn't select any image for any of titles the posted data would be like this:
title = ["title1","title2","title3"]
images = [image1.jpg,image3.jpg]
the count of users is not fixed and user can add any desired number of titles and images by clicking on the add new user button. so "add new user" button will add a pair of these inputs to users div:
<input type="text" name="title[]" required>
<input type="file" name="images[]">
is there any way to detect if the file is selected or not? or to send a default value for not selected file inputs? I want to set null value for image if no file is selected, like this:
user1:{title="title1",image=image1.jpg}
user2:{title="title2",image=null}
user3:{title="title3",image=image3.jpg}
.
.
.
You can check if particular index is set for file:
var_dump($_POST['title'][0]);
if (isset($_FILES['images']['tmp_name'][0])) {
var_dump($_FILES['images']['tmp_name'][0]);
}
All you have to do is process the 2 arrays and check there is a matching image with each title. If there is not a image for each title, put anything that you like in place of the image name
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$output = [];
foreach( $_POST['title'] as $idx => $title){
$u = $idx+1;
$output['user'.$u] = ['title'=>$title,
'images'=> $_POST['images'][$idx] == '' ? 'NULL' : $_POST['images'][$idx]];
}
$j = json_encode($output);
echo $j;
}
RESULT
{
"user1":{"title":"asss","images":"NULL"},
"user2":{"title":"dd","images":"hammer.jpg"}
}
Thank you everybody, Pavol Velky was right partially, Although the array size is less than the number of inputs, but the browser will set an index to the array, in example if you have two inputs and just select the second one, the array will be like : item:[1:{}] (if you select first one: item:[{}]) and you can check the order by the index number.

Hello here i am having this problem of passing multiple data In form

As you can see here I am looping with foreach but it only sends the one product_name (last one) of the cart, but I Want to send every product_name.
<form method="POST" action="{{route('order.store')}}">
#csrf
#method('POST')
#foreach(Cart::content() as $item)
<input type="hidden" name="product_name" value="{{$item->name}}">
#endforeach
</form>
You have to use like input name as array
<input type="hidden" name="product_name[]" value="{{$item->name}}">
you have to use an array to send multiple values using the same name attribute.
#foreach(Cart::content() as $item)
<input type="hidden" name="product_name[]" value="{{$item->name}}">
#endforeach
this will send each product name and you have to loop through to get every one in the backend.

how to use the form(checkbox) to pass the user id

I have a table in it there are checkboxes I need that on those users
on which the checkbox is selected their id was transferred
my form
<input type="submit" form="qwe">
<form action="{{URL::to('othet_nn')}}" method="get" id="qwe">
<input type="checkbox" name="id">
</form>
my controller
public function chek(){
dd($_GET);
return view('admin.pages.othet_test');
}
I need to be passed the id of those users who had a check mark in the
checkbox
you can use this like
<form action="{{URL::to('othet_nn')}}" method="post" id="qwe">
<input type="checkbox" name="id" value="1">
</form>
public function check(Request $request){
dd($request->id);
return view('admin.pages.othet_test');
}
In Laravel you should use Request class. To see the whole input you can use:
request()->all()
and to get individual field you can use
request()->input('id')
I advise you to read documentation about requests

How to insert radio, checkbox and textbox value in same variable post method name using codigniter?

my view page
<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
<label>Who is usa president</label>
<input type="text" name="answer[]">
<label>Dhoni is cricket player player</label>
<input type="radio" name="answer[]" value="True">True
<input type="radio" name="answer[]" value="False">False
<label>Which is asian country</lable>
<input type="checkbox" name="answer[]" value="newyork">newyork
<input type="checkbox" name="answer[]" value="india">india
<input type="checkbox" name="answer[]" value="srilanka">srilanka
</form>
My controller page
public function add_stu_ans()
{
$values['stu_answer'] = $this->input->post('ans');
$this->Common_model->insert_record('student',$values);
$this->session->set_flashdata('message_name' , 'Your Data is Inserted');
redirect('Quiz/question');
}
And this is model
public function insert_record($table,$values) {
$this->db->insert($table,$values);
return $this->db->insert_id();
}
please anyone can tell me how to customize the code in controller
Only possible if you take another hiddent text field where your 'qstn_id' will be in value. take this field in array.
in coding part while taking foreach loop you have to take qstn_id as in "[key]" format otherwise you cant find skipped records.
for eg. :
foreach ($que as $key => $ansValue) {
$val2 = $ansValue[$key];
}
// $val2 store as your output.
This is code to save the data using json_encode. Let me know you get any error I have not run this code.
View File
<form action="<?php echo base_url()?>Quiz/add_stu_ans" method="POST">
<label>Who is usa president</label>
<input type="text" name="usa_president">
<label>Dhoni is cricket player player</label>
<input type="radio" name="cricket_player" value="True">True
<input type="radio" name="cricket_player" value="False">False
<label>Which is asian country</lable>
<input type="checkbox" name="country[]" value="newyork">newyork
<input type="checkbox" name="country[]" value="india">india
<input type="checkbox" name="country[]" value="srilanka">srilanka
</form>
Controller
public function add_stu_ans()
{
$usa_president = $this->input->post('usa_president');
$cricket_player= $this->input->post('cricket_player');
$country= $this->input->post('country');
$final_data = array(
'usa_president' => $usa_president,
'cricket_player' => $cricket_player,
'country' => $country,
);
$json_encode_data = json_encode($final_data);
$this->Common_model->insert_record('student',$json_encode_data);
$this->session->set_flashdata('message_name' , 'Your Data is Inserted');
redirect('Quiz/question');
}
Please check this at your end and time of fetch the data from the database then you need to pass that return data from the function json_decode.
$json_decode_data = json_decode($return_data_from_data_table);

Categories