Laravel Collective checkbox element for laravel many-many relationship - php

i wanna use laravel collective for my input form,im using this to input datas for pivot table in laravel eloquent many-to-many,and i wanna use input data using checkbox element(hoby),the problem is idont know why we type at the first parameter of laravelCollective as string and way we have to type as array,anyone can explain me?in theory,thanks in advance for your help
<div class="form-check">
#if (count($list_hobi)>0)
#foreach ($list_hobi as $key => $value)
<div class="checkbox">
{{Form::checkbox('hobi[]',$key,null)}}
<label>{{$value}}</label>
</div>
#endforeach
#endif
</div>

Take for example these checkboxes:
<input type="checkbox" name="food" value="apple" /> 1
<input type="checkbox" name="food" value="pear" /> 2
<input type="checkbox" name="food" value="banana" /> 3
All three have the same name. When I check all three and submit the form and see what has been submitted with dd($request->input()), the output is:
"food" => "banana"
It appears only the last input with the same name is saved, even though I selected all three.
When I instead use food[]:
<input type="checkbox" name="food[]" value="apple" /> 1
<input type="checkbox" name="food[]" value="pear" /> 2
<input type="checkbox" name="food[]" value="banana" /> 3
the output is:
"food" => array:3 [▼
0 => "apple"
1 => "pear"
2 => "banana"
]

Related

Php add input field values in database

What the best way to add values of input files and save them later on in the database.
My input fields look like these:
<input type="checkbox" name="test[]" value="1">
<input type="checkbox" name="test[]" value="10">
<input type="checkbox" name="test[]" value="100">
<input type="checkbox" name="test[]" value="1000">
<input type="checkbox" name="test[]" value="10000">
<input type="checkbox" name="test[]" value="100000">
<input type="checkbox" name="test[]" value="1000000">
So if I check the first field and send the database saves 1 and if I check the first 3 the database should save 111(1+10+100) and so on...
Edit:
So I tried out your suggestion.
Printing out the array gives this:
array:1 [▼
"test" => array:3 [▼
0 => "1"
1 => "10"
2 => "100"
]
]
But if I array_sum that and print out the value I get a 0.
Try this
$sum = 0;
if(isset($_POST['test']) && is_array($_POST['test'])){
$sum = array_sum(array_map('intval', $_POST['test']));
}

get the right result for inserting into the database - Laravel

Im trying to store data in this table:
newspaper_id
article_id
comment (string)
This is how my HTML looks like:
<div class="newspaper-options">
<input class="form-control" name="data[]" type="text">
<input type="checkbox" class="article-option" name="article_id[]" value="3"> Active
</div>
<div class="newspaper-options">
<input class="form-control" name="data[]" type="text">
<input type="checkbox" class="article-option" name="article_id[]" value="1"> Active
</div>
This is the result im getting back when i only fill in the second newspaper options and check it:
"comment" => array:2 [▼
0 => ""
1 => "option 2"
]
"article_id" => array:1 [▼
0 => "1"
]
The problem im having is how can i group the input values so i can store it inside my table. The value of my checkbox is that of the article_id. and the text field is used to fill the comment inside the database.
Any help with this would be greatly appreciated!
The information that is missing to correctly put that info into the database is the mapping of the array position to the article_id.
E.g. put
<input name="article[]" type="hidden" value="...">
in each <div class="newspaper-options">
Like so
<div class="newspaper-options">
<input name="article[]" type="hidden" value="3">
<input class="form-control" name="data[]" type="text">
<input type="checkbox" class="article-option" name="article_id[]" value="3"> Active
</div>
<div class="newspaper-options">
<input name="article[]" type="hidden" value="1">
<input class="form-control" name="data[]" type="text">
<input type="checkbox" class="article-option" name="article_id[]" value="1"> Active
</div>
Then in your Controller you can loop through Input::get('article'), check the active checkbox and add the comment.
E.g.
foreach(Input::get('article') as $pos => $article_id){
if(in_array($article_id, Input::get('article_id')){
add $article_id and Input::get('comment')[$pos] to DB
}
}
Apart from that you should get a better name for your article_id field, like "active" and change "data" to "comment".
If you could add the input fields with keys in the first place you could handle them more easily. I guess it is possible to do
<input class="form-control" name="data[3]" type="text">
<input type="checkbox" class="article-option" name="article_id[3]" value="3"> Active

Auto Incremented Name Attribute on Radio Buttons

I have a form that has a set of radio buttons, and I am sending the value of the checked button to another page with php. The form has the option to add another set of the same radio buttons, so if I use the following method of giving name attributes:
<div>
<input type="radio" name="food[]" value="apple" /> Apple
<input type="radio" name="food[]" value="orange" /> Orange
</div>
And add a clone of the above,
$_POST['food'];
This would give me the value of each input, and that would total 4. I'm trying to use the bracket incrementing method but still keep the functionality of returning the value of the radio button that was checked.
So my question is, is there a way to use the square brackets to do auto incrementing on the name attribute, but when sending the values to php make sure each div grouping of radio buttons only returns one of the values for whichever one is checked? Please let me know if this question is too vague, this is my first question here.
May i don't understand your question. You can not Auto Incremented the name cause you use radio buttons. By Choosing apple on the first page and orange on the second you will loose your first choose (apple). You can do it with checkboxes instead of radio buttons:
Page 1:
<form method="post" action="page2.php">
<div>
<input type="checkbox" name="numberofapples" value="1" /> Apple
<input type="checkbox" name="numberoforanges" value="1" /> Orange
</div>
<input type="submit" value="go">
</form>
Page 2:
<form method="post" action="result.php">
<div>
<?if($_POST['numberofapples']){?><input type="hidden" name="numberofapples" value="1" /> <?}?>
<input type="checkbox" name="numberofapples" value="<?=($_POST['numberofapples'])?2:1?>" /> Apple
<?if($_POST['numberoforanges']){?><input type="hidden" name="numberoforanges" value="1" /> <?}?>
<input type="checkbox" name="numberoforanges" value="<?=($_POST['numberoforanges'])?2:1?>" /> Orange
</div>
<input type="submit" value="go">
</form>
Result page:
Apples: <?=($_POST['numberofapples'])?$_POST['numberofapples']:0?>
Oranges: <?=($_POST['numberoforanges'])?$_POST['numberoforanges']:0?>
But why don't you do the counting after posting your second page? (add the post values from page 1 as hidden fields to the form of page 2)
If you want to use several div groups with the same input names, I would create them like this:
<div>
<input type="radio" name="food[0][]" value="apple" /> Apple
<input type="radio" name="food[0][]" value="orange" /> Orange
</div>
<div>
<input type="radio" name="food[1][]" value="apple" /> Apple
<input type="radio" name="food[1][]" value="orange" /> Orange
</div>
<div>
<input type="radio" name="food[2][]" value="apple" /> Apple
<input type="radio" name="food[2][]" value="orange" /> Orange
</div>
So, in your PHP, you will have each group in separated keys. For example
print_r( $_POST['food'] );
Should return something like this:
array(
[0] => array(
[0] => 'apple'
),
[1] => array(
[0] => 'orange'
),
[2] => array(
[0] => 'apple',
[1] => 'orange'
)
)
)

form with 2 radios for each user in php

I have a form that dynamically prints out names based on a unique name tag.
To be used for responding on invitations. There can be multiple users for each name tag, i.e. couples, couples with children, etc.
For each name there is a radio-button where the invitee can select whether he/she is coming to the party or not (has the value 1=not coming, 2=coming). It can look like this, for a couple:
Lady Mary S
I am not coming - radio - value 1
I am coming - radio - value 2
Mister Hans S
I am not coming - radio - value 1
I am coming - radio - value 2
Child of Mary & Hans S
I am not coming - radio - value 1
I am coming - radio - value 2
Each of these invitees has a user id - I need the reponse to be with the user id.
The HTML for it looks like this:
<div>
<h1>Lady Mary S</h1>
<label for="no-1">I am not coming</label>
<input type="radio" value="1" id="no-1" name="response[1]" /><br />
<label for="yes-1">I am coming</label>
<input type="radio" value="2" id="yes-1" name="response[1]" />
<input type="hidden" name="userid[]" value="1" />
</div>
<div>
<h1>Mister Hans S</h1>
<label for="no-2">I am not coming</label>
<input type="radio" value="1" id="no-2" name="response[2]" /><br />
<label for="yes-2">I am coming</label>
<input type="radio" value="2" id="yes-2" name="response[2]" />
<input type="hidden" name="userid[]" value="2" />
</div>
<div>
<h1>Child of Mary & Hans S</h1>
<label for="no-3">I am not coming</label>
<input type="radio" value="1" id="no-3" name="response[3]" /><br />
<label for="yes-3">I am coming</label>
<input type="radio" value="2" id="yes-3" name="response[3]" />
<input type="hidden" name="userid[]" value="3" />
</div>
These names will be fetched from MySQL with a unique nametag for these 3 people, f.x. MARHAN
This is a dynamic form - for some nametags there will only be two invitees and for other nametags there will be 3 or 4, etc.
My problems is that I cannot come up with a solution on how to handle the posted data so I can make sure the reponse is for the right user id.
All you need is
foreach($_POST['response'] as $userid => $value) {
echo $userid.'-'.$value."<br>";
}
Because you already have userid in key of radio button name name="response[1]"
Just an idea: How about making the value of each input something like 'answer-userId'. e.g.:
<input type="radio" value="1-333" id="no-3" name="response[]" /><br />
Then you can split the posted value in php by '-' and get the answer (1) and user id (333).

Put checked checkboxes into an array

TO CLARIFY I'VE GOT THE VALUES OF THE CHECKED CHECKBOXES. I NEED TO ORGANIZE THEM IN AN ARRAY SIMILAR TO THE PHP EXAMPLE GIVEN. THANKS
I'm trying to build an array of checked checkboxes to be sent over AJAX for processing by PHP. Originally I got the selected checkboxes by PHP but I'm having trouble converting this process to Javascript.
There are three different groups of checkboxes ('first', 'second', 'third') and for each of these groups there are 4 different checkboxes that can be checked.
The html of the checkboxes followings the same pattern up to value 4.
<input type="checkbox" name="first" class="selection first" value="1" />
<input type="checkbox" name="second" class="selection second" value="1" />
<input type="checkbox" name="third" class="selection third" value="1" />
<input type="checkbox" name="first" class="selection first" value="2" />
<input type="checkbox" name="second" class="selection second" value="2" />
<input type="checkbox" name="third" class="selection third" value="2" />
and so on....
The PHP code would return an array of the checked checkboxes like such.
$selections => array(
['first'] => array(
[0] => 1,
[1] => 3,
[2] => 4),
['second'] => array(
[0] => 2),
['third'] => array(
[0] => 1,
[1] => 4)
);
I've been trying and trying to replicate this in JavaScript but I keep getting errors such as cannot convert undefined to object and the likes. I loop the available positions to create arrays, then loop all the checkboxes and if checked, I get the value.
console.log() prints the position ('first', 'second' or 'third') and the number of the selection (1 - 4). That's all I need, but how do I put this in an array similar to the PHP one?
function getSelections() {
var selections = new Array();
$('.position').each(function() {
selections[$(this).attr('value')] = new Array;
});
$('.selection').each(function() {
if( $(this).prop('checked') == true ) {
position = $(this).attr('name');
selection = $(this).attr('value');
console.log(position + ' - ' + selection);
}
});
return selections;
}
It's to be sent to a PHP script as JSON and I need to decode it as an array like the PHP one.
Nothing fancy needed.... change to this and view the posted arrays.
<input type="checkbox" name="first[]" class="selection first" value="1" />
<input type="checkbox" name="second[]" class="selection second" value="1" />
<input type="checkbox" name="third[]" class="selection third" value="1" />
<input type="checkbox" name="first[]" class="selection first" value="2" />
<input type="checkbox" name="second[]" class="selection second" value="2" />
<input type="checkbox" name="third[]" class="selection third" value="2" />
Then serialize this as normal to submit via ajax instead.
Jquery :checked Selector may help

Categories