Printing array to blade - php

I'm getting an error of
htmlspecialchars() expects parameter 1 to be string, object given.
I'm trying to print an array from session to blade.
view:
<input type="text" name="to" value="{{$mail}}">
controller:
public function view_send_email()
{
$data["_email_list"] = Tbl_press_release_email::get();
$data["sent_email"] = Request::input('sent_email');
$mail = Session::get('email');
return view("send_email", compact('data', 'mail'));
}

You should try this:
#foreach ($mail as $email)
<input type="text" name="to[]" value="{{$email}}">
#endforeach
Note: As you will have multiple values in $email you need to take array of input element as mentioned in above code (i.e name = "to[]")
Updated Answer
#foreach ($mail as $email)
#foreach ($mail as $emails)
<input type="text" name="to[]" value="{{$emails}}">
#endforeach
#endforeach

<input type="text" name="to" value="{{$mail}}">
To
<input type="text" name="to" value="{{print_r($mail)}}">

It seems like it's returning multiple values, so you have to loop through them to display all of them, use a foreach loop.
#foreach ($mail as $email)
<input type="text" name="to" value="{{$email}}">
#endforeach
If you want Form Model Binding
That's a different thing but the same concept, you can view the docs here.
EDIT: It looks like you want to store an array into an input, to do this you must add a [] at the end of the name of your input like this
<input type="text" name="to[]" value="{{$mail}}">
Then when they submit you simply go Input::get('to')[0] to display the first input.

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!

Checkbox data isn't being stored to DB

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.

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.

php Laravel adding inputs with name as an array

I am trying to create a form where the names of the inputs are an array. I am going through an array sent from another view to get fields to show. in this example I want to show 3 fields.
$someResult = array('0','1','2');
$fields = array(0=>'fName',1=>'mName',2=>'lName');
#foreach($someResult as $k){
<td> {!! Form::text($fields[$k][],$someVal) !!}</td>
#endforeach
I tried simplifing it to straight php:
foreach ($someResult as $k){
echo "<tr><td><input type='text' name='".$fields[$k][]."' value='".$someVal."'></tD></tr>";
}
Either way, i get the error "Cannot use [] for reading"
How can i declare name argument as an array?
My goal is something like:
<td>
<input type='text' name='fName[]' value='someVal'>
</td>
<td>
<input type='text' name='mName[]' value='someVal'>
</td>
//etc.....
The square bracket should be used as a string in your dom.
#foreach ($someResult as $k)
<tr>
<td>
<input type="text" name="{{ $fields[$k] }}[]" value="{{ $someVal }}">
</td>
</tr>
#endforeach

Creating an Array with PHP

I have a form with multiple text inputs that all have the same name. How would I process that with my PHP when the user submits the form?
HTML:
<input type="text" name="interest"/>
I've assumed you're using POST.
You would use
<input type="text" name="interest[]">
Then on the post page, you could use:
foreach($_POST['interest'] as $i){
echo $i. "<br>";
}
or whichever method you wanted to use to get the POST data.
You could also do something like:
<input type="text" name="interest[music]"/>
<input type="text" name="interest[food]"/>
You can then call this data by using:
<?php echo $_POST['interest']['music']; ?>
<input type="text" name="interest[]"/>
You should add square brackets. This triggers PHP to put them in an array like this:
HTML
<input type="text" name="interest[]"/>
<input type="text" name="interest[]"/>
PHP
//Get values
var_dump($_POST['interest']);
Use brackets in your input field to create an array of POST values:
<input type="text" name="interest[]"/>
<?php
var_dump($_POST['interest']); // will be an array
?>

Categories