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'
)
)
)
Related
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"
]
goal: post several groups of radio buttons to array (as a message, to database, etc.)
Hi there,
I was wondering how i can post my selections of several groups of radio buttons.
Here is the code:
<body>
<form action="" method="post">
<select name="module 1">
<input type="radio" name="radioA" value="Radio 1">Radio 1
<input type="radio" name="radioA" value="Radio 2">Radio 2
<input type="radio" name="radioA" value="Radio 3">Radio 3
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 2">
<input type="radio" name="radioB" value="Radio 21">Radio 21
<input type="radio" name="radioB" value="Radio 22">Radio 22
<input type="radio" name="radioB" value="Radio 23">Radio 23
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 3">
<input type="radio" name="radioC" value="Radio 31">Radio 31
<input type="radio" name="radioC" value="Radio 32">Radio 32
<input type="radio" name="radioC" value="Radio 33">Radio 33
<input type="submit" name="submitC" value="Get Selected Values" />
</select>
<br>
</form>
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['radioA']))
{
echo "You have selected :".$_POST['radioA']; // Displaying Selected Value
}
}
?>
</body>
...But I can't make it display the list of my selections, but only one instructions, instead of 3x (one for each).
Long story short... How can I treat this as a 1 global set of 3 groups, having only 1 "submit" button to process all 3 groups at once?
Txs a lot, and have a nice weekend.
You just need to remove the other two submit buttons and only display one. As long as it is inside the <form> it will submit all the radio button "groups" to the $_POST.
Also, remove the select tags as you are using them incorrectly. Only an option or optgroup should be nested inside a select element.
You will need to reference each one individually to see the selected response:
echo $_POST['radioA'];
echo $_POST['radioB'];
echo $_POST['radioC'];
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).
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
So that in PHP I can deal with them as :
foreach($_POST['checkboxname'] as $i => $value)
...
Do something like this:
<input type="checkbox" name="checkboxArray[]" />
Note the [] in the name.
Like this:
<input type="checkbox" name="checkboxname[]" />
<input type="checkbox" name="checkboxname[]" />
<input type="checkbox" name="checkboxname[]" />
<input type="checkbox" name="checkboxname[]" />
<input type="checkbox" name="checkboxname[]" />
Just append [] to their names.
If you use an array for the checkboxes, you should add a value option as identifier for the single checkboxes, because then the returned array changes from
Array ( [0] => on, [1] => on) to Array ( [0] => value1, [1] => value5 ), which let you identify the checked checkboxes.
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(even){
$("button").click(function(){
var checkvalue = [];
$.each($("input[name='1']:checked"), function(){
checkvalue.push($(this).val());
});
alert("checkvalue: " + checkvalue.join(", "));
});
});
</script>
<body>
<input type="checkbox" name="1" value="1" > 1 <br/>
<input type="checkbox" name="1" value="2"> 2 <br/>
<input type="checkbox" name="1" value="3"> 3 <br/>
<button type="button">Get Values</button>
</body>
</html>
for those HTML form elements that can send multiple values to server (like checkboxes, or multiple select boxes), you should use an array like name for your HTML element name. like this:
<input type="checkbox" name="checkboxname[]" />
also it is recommended that you use an enctype of "multipart/form-data" for your form element.
<form enctype="multipart/form-data" action="target.php" method="post">
then in your PHP scripts you can access the multiple values data as an array, just like you wanted.