Please Help. Need help with counting variables in php

Can somebody help me via counting variables after a user clicks a submit button? Basically I have two checkboxes that the user can check. After checking a box or boxes, they hit submit and the rubrics chosen displays the number of rubrics chosen. Under each of these rubrics are a list of remedies under these rubrics. What I need help with is counting the remedies. Say they choose Mind-Anger, the remedy Aconite comes up and would count as 1. Say they choose two rubrics: Mind-Anger and Mind-Anger(Irascibility), then aconite should come up with a 2 count. Right now I’m having trouble with this. Please help me. Thanks. My code is below:

[php]

<?php $Aconite[0] = 1; $Aconite[1] = 1; ?>

Choose Rubrics to Help Find the Remedy:

Mind - Anger(Irascibility)

Mind - Anger

<?php if (isset($_POST['submit'])) { $rubric = $_POST["rubric"]; $how_many = count($rubric); echo 'Number of Rubrics Chosen: '.$how_many.'

'; if ($how_many>0) { echo 'You chose the following rubrics:
'; } for ($i=0; $i<$how_many; $i++) { echo ($i+1) . '- ' . $rubric[$i] . '
'; } echo "

"; } $result = count($Aconite); echo ($result); ?> [/php]

So all you want to know which check box or boxes they clicked on?

Your form is wrong for one, you don’t want to use html inside the value.

<form method="post" action="">
 Choose Rubrics to Help Find the Remedy:<br /><b /r>
 <input type="checkbox" name="rubric[]" value="Mind - Anger(Irasibility)" /><?=$Aconite[0]?>">Mind - Anger(Irascibility) <br />
 <input type="checkbox" name="rubric[]" value="Mind - Anger>">Mind - Anger <b r />
 <input type="submit" name = "submit" value="submit">
</form>

Here is really good tuturial on this particular topic, used it many times myself - http://www.html-form-guide.com/php-form/php-form-checkbox.html. Explains different ways of handling checkboxes. Personally, i’d go with the first way since you only have 2 boxes.

Hi Richei,

First Thank you for replying. I appreciate this greatly. My main problem is the php part where I need to add up the remedies (Aconite) after the user hits the submit button. Basically my form has 2 check boxes with 2 different rubrics(mind-anger) and mind-anger(irascibility). Say the user only checks 1 of those checkboxes then Aconite would count as 1. If they check both boxes then aconite should count as 2. Basically I was going to add more checkboxes with different remedies and I need a numerical score by each of those remedies. I don’t know if I should create an array or declare a variable for each of the remedies. This is where I’m stuck in the php. I really appreciate your help.

Thanks again,

Cody

Look at thst link I posted, it tells u near the end

Hi Richei,

Got it to work. Thank you!

Cody

Glad you got it working :slight_smile: come back if you need anything else

Sponsor our Newsletter | Privacy Policy | Terms of Service