I've been playing with trying to get the first 4 of a checkbox array to act like radio buttons for a while now and I thought I had it but I can't figure it out.. I searched through here and found..
Checkbox to act like radio button, check first four or last four checkboxes but not both- javascript?
Though I've haven't quite been able to make it work in my case apparently. So to re-iterate. I need the first 4 you can only pick 1, while the others can be multiple of any combination. I know its something small I have to be missing.
HTML
<form name="account_info" id="account_info">
<input type="checkbox" name="permissions[]" value="0" onClick="checkOnly(this)"> Permission 0<br>
<input type="checkbox" name="permissions[]" value="1" onClick="checkOnly(this)"> Permission 1<br>
<input type="checkbox" name="permissions[]" value="2" onClick="checkOnly(this)"> Permission 2<br>
<input type="checkbox" name="permissions[]" value="3" onClick="checkOnly(this)"> Permission 3<br>
<input type="checkbox" name="permissions[]" value="4"> Permission 4<br>
<input type="checkbox" name="permissions[]" value="5"> Permission 5<br>
<input type="checkbox" name="permissions[]" value="6"> Permission 6<br>
<input type="checkbox" name="permissions[]" value="7"> Permission 7<br>
</form>
The Script
function checkOnly(myCheckbox) {
for(var i = 0; i < 4; i++) {
if(document.account_info.elements[i].name != myCheckbox.name) {
document.account_info.elements[i].checked = false;
}
}
}
Here is the link to jsfiddle
http://jsfiddle.net/np4dry0f/3/
The problem is that the name attribute will always be permissions[] in javascript.
A possible solution is to use the value instead of the name:
function checkOnly(myCheckbox) {
for(var i = 0; i < 4; i++) {
if(document.account_info.elements[i].value != myCheckbox.value) {
document.account_info.elements[i].checked = false;
}
}
}
See an example on JS Bin.
Much easier than looping through each checkbox
$('.radio').change(function() {
$('.radio').not(this).removeAttr('checked');
$(this).attr('checked','checked');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form name="account_info" id="account_info">
<input class="radio" type="checkbox" name="permissions[]" value="0"> Permission 0<br>
<input class="radio" type="checkbox" name="permissions[]" value="1"> Permission 1<br>
<input class="radio" type="checkbox" name="permissions[]" value="2"> Permission 2<br>
<input class="radio" type="checkbox" name="permissions[]" value="3"> Permission 3<br>
<input class="radio" type="checkbox" name="permissions[]" value="4"> Permission 4<br>
<input class="radio" type="checkbox" name="permissions[]" value="5"> Permission 5<br>
<input class="radio" type="checkbox" name="permissions[]" value="6"> Permission 6<br>
<input class="radio" type="checkbox" name="permissions[]" value="7"> Permission 7<br>
</form>
Related
I am working on a custom survey that is dynamically populated with database information and needs to be saved as an array. I can make everything work as it should except I would like to use a star rating system for many of the answers and it seems radio buttons won't work like this:
<input type="radio" name="surveyRating[]" value="1" />
<input type="radio" name="surveyRating[]" value="2" />
<input type="radio" name="surveyRating[]" value="3" />
<input type="radio" name="surveyRating[]" value="4" />
<input type="radio" name="surveyRating[]" value="5" />
Because there are multiple instances of this same code. This will be saved into an array so surveyRating will be used over and over again. If I click a radio button in one group, it changes to a radio in another group.
I have read I can possibly do this with checkboxes instead. Is that the best alternative? Or is there another option I should be looking at? I want the final product to be star ratings 1-5. Not sure if I can do that with checkboxes.
EDIT
Ok so using name="surveyRating[1]" is helping as far as the radio buttons not conflicting on the client side. However, it is not saving correctly the way I have my php set up now. Here is how it is saved currently. What do I need to change to make the [1] work appropriately. Currently it is only saving the last iteration.
$new = array();
$ratings = $_POST['surveyRating'];
$count = count( $ratings );
for ( $i = 0; $i < $count; $i++ ) {
if ( $ratings[$i] != '' ) {
$new[$i]['surveyRating'] = stripslashes( strip_tags( $ratings[$i] ) );
}
}
EDIT 2
So to demonstrate how I accomplished this using the answer below, I had to add [0] as the first iteration in the loop. I was using $items = 0; $items++ to dynamically add a number to each loop.
To make it start at 0, I set $items = -1 that way the first iteration is 0 instead of 1. Hope that makes sense.
Yep, it does because you have the name with []. That's for array. Remove it and you will get it without array:
<input type="radio" name="surveyRating" value="1" />
<input type="radio" name="surveyRating" value="2" />
<input type="radio" name="surveyRating" value="3" />
<input type="radio" name="surveyRating" value="4" />
<input type="radio" name="surveyRating" value="5" />
For radio buttons with several groups, you have to do something like this:
<!-- Group 1 -->
<input type="radio" name="surveyRating[1]" value="1" />
<input type="radio" name="surveyRating[1]" value="2" />
<input type="radio" name="surveyRating[1]" value="3" />
<input type="radio" name="surveyRating[1]" value="4" />
<input type="radio" name="surveyRating[1]" value="5" />
<!-- Group 2 -->
<input type="radio" name="surveyRating[2]" value="1" />
<input type="radio" name="surveyRating[2]" value="2" />
<input type="radio" name="surveyRating[2]" value="3" />
<input type="radio" name="surveyRating[2]" value="4" />
<input type="radio" name="surveyRating[2]" value="5" />
I have an HTML form that I need to then reference to in PHP, so that I can eventually filter through data. Right now it is just echoing a bit of text for testing purposes.
I use the $_GET variable to get which values are equal to what, then use an if/else statement to tell me if each is checked.
If I say it only equals one value (== .25) it returns false.
However if I add one more or both more values (== .25 or .375 or .5) it will return the value I need.
How can I get it to return true with only one value?
<table stlye="width:100%">
<tr>
<td style="width:50%">
<form method="GET">
Tool Diameter: <br>
<input type="checkbox" name="Tool Diameter" value=.25 checked> .25<br>
<input type="checkbox" name="Tool Diameter" value=.375 checked> 3/8<br>
<input type="checkbox" name="Tool Diameter" value=.5 checked> 1/2<br><br>
Brand: <br>
<input type="checkbox" name="Brand" value="Lakeshore Carbide " checked> Lakeshore Carbide<br>
<input type="checkbox" name="Brand" value="AB Tools" checked> AB Tools<br>
<input type="checkbox" name="Brand" value="Helical Tools" checked> Helical Tools<br><br>
Flutes: <br>
<input type="checkbox" name="Flutes" value="2" checked> 2<br>
<input type="checkbox" name="Flutes" value="3" checked> 3<br>
<input type="checkbox" name="Flutes" value="4" checked> 4<br><br>
Tool Material: <br>
<input type="checkbox" name="Material" value="HSS" checked> HSS<br>
<input type="checkbox" name="Material" value="Carbide" checked> Carbide<br>
<input type="checkbox" name="Material" value="Cobalt" checked> Cobalt<br><br>
Coating: <br>
<input type="checkbox" name="Coating" value="Uncoated" checked> Uncoated<br>
<input type="checkbox" name="Coating" value="ZrN" checked> ZrN<br>
<input type="checkbox" name="Coating" value="TiCN" checked> TiCN<br><br>
Tool Type: <br>
<input type="checkbox" name="Type" value="Face Mill" checked> Face Mill<br>
<input type="checkbox" name="Type" value="Flat Endmill" checked> Flat Endmill<br>
<input type="checkbox" name="Type" value="Ball Endmill" checked> Ball Endmill<br>
<br><button>Filter</button><br>
</form>
</td>
<td style="width:50%">
<style type="text/css">
td
{
padding:0 50px 0 50px;
}
</style>
<?php
//while (true){
if ($_GET['Tool Diameter'] == .375) {
echo 'test = true';
}
else {
echo "false";
}
?>
</td>
</tr>
</table>
Convert the values to strings e.g.
<input type="checkbox" name="Tool Diameter" value=".375" checked> .375<br>
then check
if ($_GET['Tool Diameter'] == ".375"){
enter code here
}
try this
<input type="checkbox" name="Tool Diameter[]" value=".25" checked> .25<br>
<input type="checkbox" name="Tool Diameter[]" value=".375" checked> 3/8<br>
<input type="checkbox" name="Tool Diameter[]" value=".5" checked> 1/2<br><br>
.....
<?php
// for checking the condition 'atleast 1 ' should be checked
if(sizeof($_GET['Tool Diameter']) >=1){
echo 'test = true';
}
else {
echo "test = false";
}
?>
1st mistake
values have to be wrapped in parenthesise value=".25"
2nd mistake
names have to be unique, as a result you have only one value in the array $_GET['Tool Diameter']
you should have a slot in $_GET that contains i.e. array of your results, so lets say
$_GET['Dimensions'] = [
'Dimension 1' => '.25',
'Dimension 2' => '.375',
'Dimension 3' => '.5'
];
and then refer to each of them separately
I have a form, but I can not receive the data correctly.
I want to receive the id and the option you chose, how can I do this?
I tried to foreach, but could not.
<input type="hidden" id="Id_field[]" name="Id_field[]" value="1"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No
<input type="hidden" id="Id_field[]" name="Id_field[]" value="2"/>
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="yes" />Yes
<input type="checkbox" id="option[]" name="option[]" class="validate[required]" value="no" />No
Receiving correctly:
Id_field = 1
option = yes
Id_field = 2
option = no
First of all, id's need to be unique, so this isn't valid (but that doesn't matter for the question). What you can do is use the id in the name like this
<input type="checkbox" name="option[1]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[1]" class="validate[required]" value="no" />No
<input type="checkbox" name="option[2]" class="validate[required]" value="yes" />Yes
<input type="checkbox" name="option[2]" class="validate[required]" value="no" />No
Now you will know by the value if yes or no has been clicked and by the key the id. Also you might consider changing to radiobuttons, then one has to chose either one but can't answer both.
I'm new to this. I'm studying web development and have to create a php response form for a questionnaire which then inputs into a database. I'm having trouble with the radio buttons. I can't create the right code that makes an array and displays the answers in the response form/page.
This is my code:
<form name="modulequestionnaire" method="post" action="tania.responseform.php" />
<p><i>Rate each question from 6 to 1, six being strongly
agree and one being strongly disagree.</i></p>
1. I think the module guide/student handbook provided enough information about the
module content, organisation and assessment.<br/>
6<input type="radio" name="answer[1]" value="6"> 5<input type="radio" name="answer[1]" value="5">
4<input type="radio" name="answer[1]" value="4"> 3<input type="radio" name="answer[1]" value="3">
2<input type="radio" name="answer[1]" value="2"> 1<input type="radio" name="answer[1]" value="1">
</p>
2.The module was well organised.<br/>
6<input type="radio" name="answer[2]" value="6"> 5<input type="radio" name="answer[2]" value="5">
4<input type="radio" name="answer[2]" value="4"> 3<input type="radio" name="answer[2]" value="3">
2<input type="radio" name="answer[2]" value="2"> 1<input type="radio" name="answer[2]" value="1">
</p>
3.The Learning Resource Centre provided adequate materials for the module.<br/>
6<input type="radio" name="answer[3]" value="6"> 5<input type="radio" name="answer[3]" value="5">
4<input type="radio" name="answer[3]" value="4"> 3<input type="radio" name="answer[3]" value="3">
2<input type="radio" name="answer[3]" value="2"> 1<input type="radio" name="answer[3]" value="1">
</p>
I know the answer can relate to the isset function but I don't know how to code it.
Could someone possibly teach or help me out here?
When you're unsure of how to handle the HTML markup that you've set up, you should var_dump($_POST) the values that are sent to the PHP handler page so you know what the format will look like, that way you can proceed from there.
When I created your HTML and tested it with a var_dump and some random selections, the output was
array(2) { ["answer"]=> array(3) { [1]=> string(1) "5" [2]=> string(1) "3" [3]=> string(1) "4" } ["submit"]=> string(6) "Submit" }
Notice that there is an array within the $_POST['answer'] variable. So you should foreach over each element in that array to handle each respective value:
foreach ($_POST['answer'] as $answer) {
// do stuff with the answer
}
If you need to work with the answer number that you defined in the POST array, you can foreach with a key:
foreach ($_POST['answer'] as $answerNum => $answer) {
// do stuff with $answerNum and $answer
}
You can, of course, access your answer by its number directly:
if (!empty($_POST['answer'][1])) { // To ensure that the value is being sent
// do stuff with $_POST['answer'][1]
}
I don't imagine that this is quite what you are looking to do. If instead you give the three questions different names:
<form name="modulequestionnaire" method="post" action="tania.responseform.php" />
<p><i>Rate each question from 6 to 1, six being strongly
agree and one being strongly disagree.</i></p>
1. I think the module guide/student handbook provided enough information about the
module content, organisation and assessment.<br/>
6<input type="radio" name="answer1" value="6"> 5<input type="radio" name="answer1" value="5">
4<input type="radio" name="answer1" value="4"> 3<input type="radio" name="answer1" value="3">
2<input type="radio" name="answer1" value="2"> 1<input type="radio" name="answer1" value="1">
</p>
2.The module was well organised.<br/>
6<input type="radio" name="answer2" value="6"> 5<input type="radio" name="answer2" value="5">
4<input type="radio" name="answer2" value="4"> 3<input type="radio" name="answer2" value="3">
2<input type="radio" name="answer2" value="2"> 1<input type="radio" name="answer2" value="1">
</p>
3.The Learning Resource Centre provided adequate materials for the module.<br/>
6<input type="radio" name="answer3" value="6"> 5<input type="radio" name="answer3" value="5">
4<input type="radio" name="answer3" value="4"> 3<input type="radio" name="answer3" value="3">
2<input type="radio" name="answer3" value="2"> 1<input type="radio" name="answer3" value="1">
</p>
then in php all you will need to do is find the values using the $_POST variable as such
<?php
echo $_POST['answer1'];
echo $_POST['answer2'];
echo $_POST['answer3'];
?>
How can I process the checkboxes only if they're checked and grab the value of the checked ones only.
php
if (is_array($_POST['add'])) {
foreach ($_POST['add'] as $key => $value) {
$_POST['add'][$key] = mysql_real_escape_string(stripslashes($value));
}
}
html
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[wmeet]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[wmeet]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[wmeet]"
class="checkbox" />
Only checked checkboxes are ever presented to PHP, so your PHP code is correct.
However, your HTML isn't correct, as all your checkboxes have the same name. This means PHP will only ever see one of them.
To get an array of checkboxes you either need to give your checkboxes unique names like this
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[ce]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[sf]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[sm]"
class="checkbox" />
Or use the empty box technique like this.
<input type="checkbox" id="wmeet_ce"
value="ce"
name="add[]"
title="Wanting To Meet"
class="checkbox {validate:{required:true,minlength:1}}"/>
<input type="checkbox" id="wmeet_sf"
value="sf"
name="add[]"
class="checkbox"/>
<input type="checkbox" id="wmeet_sm"
value="sm"
name="add[]"
class="checkbox" />