Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

handling values form a multiple checkboxes 1

Status
Not open for further replies.

sophielois

Technical User
Sep 8, 2005
66
GB
Hi,

im trying to display the variables from a form which uses checkboxs'. The user is only allowed to select 2 checkboxes.(thankyou guys in the java forum)
How do i grab the 2 values that have been checked and turn them into 2 variables and display them?

the form
Code:
<script language="javascript" type="text/javascript">
<!-- Script checks that only 2 optional units can be selected

var totalCBs = 0;
var maxChecked = 2;

function keepTrack(c) {
    totalCBs += c.checked ? 1 : -1;
    enableDisable( c.form, totalCBs == maxChecked );
}

function enableDisable(f, b) {
    var e = f.elements;
    for (var i = 0; i < e.length; i++) {
        if (e[i].type == 'checkbox' && !e[i].checked) e[i].disabled = b;
    }
}

function doSubmit() {
    if (totalCBs!=maxChecked) {
        alert('You need to select ' + maxChecked + ' Optional Units');
        return false;
    }
    return true;
}
-->
</script>
    <form action="add3.php" method="post" onsubmit="return doSubmit();">
        <input type="checkbox" name="cb1" value="HSC25" onclick="keepTrack(this);" /> HSC25<br />
        <input type="checkbox" name="cb2" value="HSC210" onclick="keepTrack(this);" /> HSC210<br />
        <input type="checkbox" name="cb3" value="HSC21314" onclick="keepTrack(this);" /> HSC213-14<br />
        <input type="checkbox" name="cb4" value="HSC218" onclick="keepTrack(this);" /> HSC218<br />
        <input type="checkbox" name="cb5" value="HSC221" onclick="keepTrack(this);" /> HSC221<br />
        <input type="checkbox" name="cb6" value="HSC223" onclick="keepTrack(this);" /> HSC223<br />
        <input type="checkbox" name="cb7" value="HSC226" onclick="keepTrack(this);" /> HSC226<br />
		<input type="hidden" name="userid" value="<?= $userid ?>">
	    <input type="hidden" name="award" value="<?= $award ?>">
        <input type="submit" name="add" value="Proceed">
    </form>

add3.php
Code:
<?php
        $award= $_POST['award'];
        $userid= $_POST['userid'];
        $unita= $_POST['cb1'];
        $unitb= $_POST['cb2'];
        $unitc= $_POST['cb3'];
        $unitd= $_POST['cb4'];
        $unite= $_POST['cb5'];
        $unitf= $_POST['cb6'];
        $unitg= $_POST['cb7'];

echo " /* Display the 2 units that were checked*/;

}
?>

Im pretty sure i will need to create some kind of loop, but im not sure how to do this.

I need to finish up with something like:
Code:
<? echo " The user selected unit $unitf and unit $unita"; ?>

thankyou for any advice you can all offer

Sophx
 
Instead of naming your checkboxes cb1, cb2 etc., give them all the name cb[]. This will post the results as a single array.

In add3.php you can then use this:

Code:
$selected = $_POST['cb']

The variable $selected now contains the array of values from the checked boxes.

You can then loop through the array:

Code:
foreach($selectd as $result)
  {
    echo "$result</br>";
  }

Remember that with your form submitting the way it is now, users with javascript turned off will not be able to submit the form. A better way might be to use javascript that checks how many checkboxes have been selected as they are being selected rather than on submit. That way javascript users would still be only able to select 2 boxes. You could then ensure that users with js turned off had only selected 2 boxes using PHP.

To check that only 2 have been selected using PHP you can test the length of the array:

Code:
 if(sizeof($selected) > 2)
   {

   }
   
[code]
 
Thanks Dweezel,

Thats really helpful,

How can i go on and display the values in the array individually?

thanks again

Sophx
 
My mistake. I just had a proper look at your code and you are keeping track of the number of checkboxes as they're being checked.

I made a few typos as well:
Code:
$selected = $_POST['cb'];



foreach($selected as $result)
 {
   echo "$result</br>";
 }
 
You can use them directly from the array Sophie:

$selected[0] is your first result and $selected[1] is your second.
 
ok,

star for you, thanks for explaining it really well!!

I have one more little question ;)

i want to do this

foreach($selected as $result)
{
echo "$result</br>";
}

and display $result here
Code:
			echo "<tr> ";
			echo "<td colspan=\"2\"><font face=\"Verdana, Arial, Helvetica, sans-serif\" size=\"-1\"><b>/* Display $result here */</b></font></td>";
			echo "</tr>";

the tr is a snippet of code taken from a table.

Im not sure where to put everything so it will work.

Thanks again

Soph
 
To add to that.

I keep getting Parse error: parse error, unexpected $

Sophx
 
Just put all the code in the foreach loop:

Code:
foreach($selected as $results)
 {
     echo "<tr>";
     echo "<td colspan='2'><font face='Verdana, Arial, Helvetica, sans-serif' size='1' ><b>".echo $results."</b></font></td>";
   echo "</tr>";

}

You don't need to do all that escaping of quotes. If the quotes that are enclosing your whole string are double quotes, you can use single quotes within that string without causing errors. And vica versa.
 
The parse error is likely to be because you missed a semi colon at the end of one of the lines of code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top