Laravel Blade some values in 2 dimensional input array missing - php

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!

Related

Combine form values under one name using GET method

I have a form using the GET method consisting of checkboxes.
This form is sending data to another page that is receiving the GET info and using it to pull info from a json api. I need to have it send the name once with all values combined into one string like this: example.com/color=RedGreenBlue
I am able to get all values combined and echoed onto the page but because they are in a foreach loop I am not able to pass them in the form. I tried below using the hidden field to pass them with no luck.
This is the method I've seen suggested but does not work for me:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
<?php
$name = $_GET['color'];
if (isset($_GET['color'])) {
foreach ($name as $color){
echo $color;
}
}
?>
<input type="hidden" name="MajorArea" value="<?php echo $color; ?>" />
</form>
Is there a way to assign one name to a group of checkboxes? Is there a way to pull the foreach loop data and use it outside a loop? Am I overlooking a way that is much easier than this?
Thanks for any advice!
I'll go out on a limb here and delete if it isn't what you're after. $_GET['color'] will be an array or it will be empty. You could also use isset:
<form action="" method="get">
Red<input type="checkbox" name="color[]" value="Red">
Green<input type="checkbox" name="color[]" value="Green">
Blue<input type="checkbox" name="color[]" value="blue">
<input type="submit" value="submit">
</form>
<?php
if (!empty($_GET['color'])) {
echo implode($_GET['color']);
}
?>

Printing array to blade

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.

PHP implode function not working right

So I am trying to email the results of a form using PHP. inside my form I have a bunch of checkboxes. The script below works when at least 1 checkbox in a group is checked. If none of the checkboxes are checked I receive the following error:
Warning: Implode() [function.implode]: Invalid augments passed in {name of php doc} on line {xxx} Array
Here is a sample of the code I'm using:
<?php
$data = array();
foreach ($_POST as $key => $value) {
$data[] = $value;
}
if(!$data[14]) //$data[14] is an array of checkbox values
{echo 'No User Selection';}
else
{echo implode(" | ", $data[14]);} //This is where the error occurs
?>
HTML code
<label for="b0" class="left">Item 1</label>
<input type="checkbox" name="b[0]" id="b0" value="Item 1"/>
<label for="b1" class="left">Item 2</label>
<input type="checkbox" name="b[1]" id="b1" value="Item 2"/>
<label for="b2" class="left">Item 3</label>
<input type="checkbox" name="b[2]" id="b2" value="Item 3"/>
ect....
Does anyone have an idea why I'm receiving this error?
Make sure the variable a) is set and b) is an array.
$data = array();
foreach ($_POST as $key => $value) {
$data[] = $value;
}
if ( !isset($data[14]) || !is_array($data[14]) ) {
echo 'No User Selection';
} else {
echo implode(" | ", $data[14]);
}
Always properly check variables using isset(), unless of course you like giant error logs! I also suggest using the $_POST keys as the keys for $data, thus making life even easier when you want to look up a specific $_POST item.
If the checkbox is not checked, it is not sent to the server.
I suggest you to do something like this:
<label for="b0" class="left">Item 1</label>
<input type="hidden" name="b[0]" value=""/>
<input type="checkbox" name="b[0]" id="b0" value="Item 1"/>
<label for="b1" class="left">Item 2</label>
<input type="hidden" name="b[1]" value=""/>
<input type="checkbox" name="b[1]" id="b1" value="Item 2"/>
<label for="b2" class="left">Item 3</label>
<input type="hidden" name="b[2]" value=""/>
<input type="checkbox" name="b[2]" id="b2" value="Item 3"/>
This way, you are sure that b[0], b[1], etc. are always sent
You are causing the problem yourself by making the data array. Just go straight to the POST array. Everything else is overcomplicating the problem.
<?php
if(!isset ($_POST['b']) || !is_array($_POST['b'])) {
echo 'No User Selection';
} else {
echo implode(" | ", $_POST['b']);
}
?>
In addition, if the content of your form changes slightly, or you want to reorder the fields then your magic number 14 will no longer work.
Your code will be unbelievably fragile unless you change it.

insert batch data array?

I want insert following data by insert_batch as in following example in database table (mysql):
HTML:
<input name="u_id[0][0]" value="76">
<input name="un[0][0]" value="1">
<input type="text" name="ue[0][0]" value="11">
<input type="text" name="up[0][0]" value="111">
<input name="u_id[1][0]" value="77">
<input name="un[1][1]" value="2">
<input type="text" name="ue[1][1]" value="22">
<input type="text" name="up[1][1]" value="222">
<input name="un[1][2]" value="3">
<input type="text" name="ue[1][2]" value="33">
<input type="text" name="up[1][2]" value="333">
PHP:
$u_id = $this->input->post('u_id');
$un = $this->input->post('un');
$up = $this->input->post('up');
$ue = $this->input->post('ue');
$data = array();
foreach ($un as $idx => $name) {
$data[] = array(
'u_id' => $u_id[$idx],
'un' => $un[$idx],
'up' => $up[$idx],
'ue' => $ue[$idx],
);
};
$this -> db -> insert_batch('units', $data);
I want insert they as this:
How should change php code and html code? what do i do?
I am assuming you are using CodeIgniter and that the name of the database table that you want to insert to is called 'units' and that its 'id' column is autoincrement.
I am basing off my solution from CodeIgniter User Guide Version 2.0.3 using a call to a helper ($this->db->insert_string()) of the Database class.
foreach ($data as $row)
{
$error_code = $this->db->insert_string('units', $row);
}
Refer to http://codeigniter.com/user_guide/database/helpers.html
The insert_string function that the Database class provides appears to take an associative array, build an INSERT statement from the elements inside, execute it and then return a numerical error code.
LOL, it's not pretty, but it might work sometimes:
<input name="u_id[]" value="76">
<input name="un[]" value="1">
<input type="text" name="ue[]" value="11">
<input type="text" name="up[]" value="111">
<input name="u_id[]" value="77">
<input name="un[]" value="2">
<input type="text" name="ue[]" value="22">
<input type="text" name="up[]" value="222">
<input name="un[]" value="3">
<input type="text" name="ue[]" value="33">
<input type="text" name="up[]" value="333">
$u_id=$this->input->post('u_id');
$un=$this->input->post('un');
$up=$this->input->post('up');
$ue=$this->input->post('ue');
for($i=0;$i<count($u_id);$i++){
for($ii=0;$ii<count($un[$i]);$ii++){
(count($un[$i])>1)?$unn=$un[$i][$ii+1]:$unn=$un[$i][$ii];
(count($ue[$i])>1)?$uen=$ue[$i][$ii+1]:$uen=$ue[$i][$ii];
(count($up[$i])>1)?$upn=$up[$i][$ii+1]:$upn=$up[$i][$ii];
$this->db->insert('units', array(//use db insert here
'u_id'=>$u_id[$i][0],
'un'=>$unn,
'ue'=>$uen,
'up'=>$upn,
));
}
}
I'd go so far as to suggest you not use it. But perhaps it might inspire someone to offer a better solution.
Cheers.

Form with variable input values PHP

I have a form which has variable input values .For example one customer has bought 5 items so all of them show another customer has bought 3 items so only three items show up.All these values are based on database
<form id="customer" class="customer" method="post" action="">
<input type="text" name="customer1" value="" >
<input type="text" name="customer2" value="" >
<input type="text" name="customer3" value="" >
</form>
Now my question how do i process a form like this where every time the number of post variables is different.
<input type="text" name="customer[]" value="" >
Something like:
$products = array();
foreach ($_POST as $key => $value) {
array_push($products, $key, $value);
}

Categories