sonnieboy Posted December 16, 2011 Share Posted December 16, 2011 Dear Experts, I am an absolute newbie; please be patient with me. I have a variable called $app['chk']. This variable has 6 questions with checkboxes. What I have been tasked to do is to print the answer or answers that a user has selected. For instance, if a user selects the first checkbox, I am to say, echo "you selected choice 1" Here is my sample code below: { echo '<div class="question">Please select one or more answers from the checkboxes below. </div>'; if ($app['chk1'] == '1') {echo ' <div class="a">you selected choice 1</div>';} if ($app['chk2'] == '2') {echo ' <div class="a">you selected choice 2</div>';} if ($app['chk3'] == '3') {echo ' <div class="a">you selected choice 3</div>';} if ($app['chk4'] == '4') {echo ' <div class="a">you selected choice 4</div>';} if ($app['chk5'] == '5') {echo ' <div class="a">you selected choice 5</div>';} if ($app['chk6'] == '6') {echo ' <div class="a">you selected choice 6</div>';} } This part is fine. What I am having problem is how to code it if user checks 2 checkboxes or more. I hope my question is not too confusing and thanks in advance for your assistance. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/ Share on other sites More sharing options...
xyph Posted December 16, 2011 Share Posted December 16, 2011 http://www.kavoir.com/2009/01/php-checkbox-array-in-form-handling-multiple-checkbox-values-in-an-array.html Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298389 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 I am sorry but I am very confused with your response. I don't think it has anything to do with my question. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298396 Share on other sites More sharing options...
scootstah Posted December 16, 2011 Share Posted December 16, 2011 It's exactly what you want. In your form, you name the related checkboxes the same thing with square brackets. So like, <input type="checkbox" name="chk[]" value="1" /> <input type="checkbox" name="chk[]" value="2" /> <input type="checkbox" name="chk[]" value="3" /> <input type="checkbox" name="chk[]" value="4" /> <input type="checkbox" name="chk[]" value="5" /> <input type="checkbox" name="chk[]" value="6" /> Now in PHP, $_POST['chk'] will be an array of all clicked-on checkboxes. So you could loop through them and echo them out like this: foreach($_POST['chk'] as $chk) { echo 'You selected choice ' . $chk . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298398 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 First of all xyph, sorry I didn't see your link. I appreciate it. scootstah, thank you for explaining it better. I do have one issue though and this is clearly my part because of the way I presented it. The checkboxes don't have the same name. It is more like: <input type="checkbox" name="chk1" value="Y" /> <input type="checkbox" name="chk2" value="Y" /> <input type="checkbox" name="chk3" value="Y" /> <input type="checkbox" name="chk4" value="Y" /> <input type="checkbox" name="chk5" value="Y" /> <input type="checkbox" name="chk6" value="Y" /> Sorry about the misinformation. Again, I can get individual choices to display the answer but when the user checks more than one boxes, then I am stumped. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298415 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2011 Share Posted December 16, 2011 The easiest way to do this would be to change the names so they are all the same and add the square brackets as shown above. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298431 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 Pikachu2000, I totally agree with you but unfortunately, someone built this app 2 years ago and I just took over the maintenance today. Unfortunately, the deadline for making these changes is tomorrow (friday) or actually today. So, I have to work with what i have so far. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298436 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2011 Share Posted December 16, 2011 There's something missing here. How do the values get assigned from the $_POST array to the $app array? Without knowing that, it's nothing but a guessing game. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298437 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 The post array is what was suggested by some of the experts who have been helping out. The code I posted follows the coding convention of the guy who wrote the code. If the 6 checkbox choices, the coding convention is, if $apps checkbox1 = 'Y' then show the answer for checkbox1. if $apps checkbox2 =='Y' then show answer for checkbox2. and so on. The issue as stated is that while this works if a user checks only one box, how do I code it if the user checks 2 or more boxes. I did apologize for initially posting wrong info when I used =='1', '2', etc when it should be 'Y'. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298441 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2011 Share Posted December 16, 2011 That doesn't answer my question at all. Post the actual, relevant php code that handles the checkbox values and post the html form. Don't type it out, copy and paste it. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298446 Share on other sites More sharing options...
xyph Posted December 16, 2011 Share Posted December 16, 2011 So you're changing the way the form's being processed, but you can't change the form itself? This doesn't make sense, unless it's an external form. About your deadline - it doesn't affect us. We're going to try to show you the right way to do this, and not provide hacked-up solutions so you can get paid sooner. In fact, I really dislike you telling volunteers trying to help you that you can't be bothered to do it right because you don't have time. It makes me want to stop helping. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298447 Share on other sites More sharing options...
Drummin Posted December 16, 2011 Share Posted December 16, 2011 What you are not including (as already stated) is the form or the processing code. Just for example, how are you converting a $_POST array into an array called $app? Here's a basic example of what you posted and notice display section is checking for the same posted "names" as in the form. Not $app[something]. <?php if(isset($_POST['submit'])){ if (isset($_POST['chk1']) && $_POST['chk1'] == 'Y') {echo ' <div class="a">you selected choice 1</div>';} if (isset($_POST['chk2']) && $_POST['chk2'] == 'Y') {echo ' <div class="a">you selected choice 2</div>';} if (isset($_POST['chk3']) && $_POST['chk3'] == 'Y') {echo ' <div class="a">you selected choice 3</div>';} if (isset($_POST['chk4']) && $_POST['chk4'] == 'Y') {echo ' <div class="a">you selected choice 4</div>';} if (isset($_POST['chk5']) && $_POST['chk5'] == 'Y') {echo ' <div class="a">you selected choice 5</div>';} if (isset($_POST['chk6']) && $_POST['chk6'] == 'Y') {echo ' <div class="a">you selected choice 6</div>';} } echo '<div class="question">Please select one or more answers from the checkboxes below. </div> <form method="post" action=""> <input type="checkbox" name="chk1" value="Y" /> Choice 1<br /> <input type="checkbox" name="chk2" value="Y" /> Choice 2<br /> <input type="checkbox" name="chk3" value="Y" /> Choice 3<br /> <input type="checkbox" name="chk4" value="Y" /> Choice 4<br /> <input type="checkbox" name="chk5" value="Y" /> Choice 5<br /> <input type="checkbox" name="chk6" value="Y" /> Choice 6<br /> <input type="submit" name="submit" value="Submit" /><br /> </form>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298454 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 xyph, read my post again. I did not say what you are accusing of saying. This is a limesurvey app that my company bought and it is NOT my own personal business and I am not trying to get it completed so I can get paid. Man, how do you come up with these? I need help but my goodness, please help if you can without leveling unfounded accusations against me. If anyone has used it, it just gives you templates, to enter your questions, export them, etc. I have not seen any of the codes if they exist. The file I am working with is a print file that, as I am told, was built by a vendor because the department that uses this limesurvey app doesn't the print functionality that came with the app. The code that I posted is what I came up by looking at the existing print code the vendor wrote. The code works great for radio buttons or one line questions. There hasn't been any instance where the answers are presented as checkboxes. This is the first example of it. In asp, you can manipuate this and I am sure it can be done too with php where you can display one checkbox response or several checkbox responses. I am grateful for the assistance. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298531 Share on other sites More sharing options...
scootstah Posted December 16, 2011 Share Posted December 16, 2011 So why doesn't the code in your first post work? It should still work if you check two boxes. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298565 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 Hi scootstah, It *does* work *if* the user checks only *one* checkbox. For instance, the user checks checkbox 1, it works as shown by the code below. if ($app['chk1'] == 'Y') {echo ' <div class="a">you selected choice 1</div>';} If the user checks checkbox 2, it works. As long as only one checkbox is selected, it works. My issue is that if the user checks 2 or more, how do I combine the the answers and echo them out? I can say something like: if ($app['chk1'] == 'Y') and if ($app['chk2'] == 'Y') and if ($app['chk3'] == 'Y') etc, then echo??? I understand the great points made by the generous experts especially as it relates to giving the checkbox same name. That's how I would have done if I had written it but unfortunately, it is not written that way and we are using templates. That's what I meant last night when I said I have to work with the hand I have been dealt. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298577 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2011 Share Posted December 16, 2011 If you're going to continue to refuse to post the actual code and actual form, you're not likely to get actual solutions. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298578 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 the other point I unintentionally forgot to make is that the print form developed by this guy has no relationship with the limesurvey app. To me, it is kind of odd that the company would ask an independent individual to come with a custom print file rather than pay someone from limesurvey to do. At least, they would have integrated with their app. So, we are basically manually copying the questions from the template into this printform and merging the question with the answer. So, I don't think the _$Post would be helpful giving that nothing is getting passed from input form to this print file. I don't know if this makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298584 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 I think there are some listening issues here. Why are you guys refusing to understand that this is LIMESurvey app? I DO NOT have form to post. I would have been glad to do so because I am the one that needs help. I don't have a form to post. The print.php files is an independent code written by someone and is unrelated to lime survey system. The code I posted is all I have and is based on radio button example on the print.php file. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298595 Share on other sites More sharing options...
xyph Posted December 16, 2011 Share Posted December 16, 2011 You may want to ask to have this moved to the 3rd party scripts forum. From a developers standpoint, most of what you're saying makes little sense. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298639 Share on other sites More sharing options...
sonnieboy Posted December 16, 2011 Author Share Posted December 16, 2011 A decent PHP developer should figure out rather easily, print out users choices be it 1 or more checkboxes choices just based on the code I have shown. { echo '<div class="question">Please select one or more answers from the checkboxes below. </div>'; if ($app['chk1'] == '1') {echo ' <div class="a">you selected choice 1</div>';} if ($app['chk2'] == '2') {echo ' <div class="a">you selected choice 2</div>';} if ($app['chk3'] == '3') {echo ' <div class="a">you selected choice 3</div>';} if ($app['chk4'] == '4') {echo ' <div class="a">you selected choice 4</div>';} if ($app['chk5'] == '5') {echo ' <div class="a">you selected choice 5</div>';} if ($app['chk6'] == '6') {echo ' <div class="a">you selected choice 6</div>';} } Like I said earlier, you don't have to assist if you can't but refrain from heaping insults at the people you claim to be assisting. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298650 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2011 Share Posted December 16, 2011 Again, not enough information. No code, no form, no explanation of how the values get assigned to the $app array, no sample of data, no helpful information at all, just a block of conditionals. All I can tell you is there is nothing syntactically wrong with what you've posted. Without the information that you've been asked for repeatedly, that's the only thing that can be said with certainty. Quote Link to comment https://forums.phpfreaks.com/topic/253276-how-do-i-display-questions-from-checkboxes/#findComment-1298654 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.