Jump to content

How do I display questions from checkboxes?


sonnieboy

Recommended Posts

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.

Link to comment
Share on other sites

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 />';
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.