Jump to content

Loop through checkboxes


kurt.santo

Recommended Posts

Am really stuck with this problem for days now. How can you go through all the checkboxes on a page to calculate the price as: up to (and inclusive) 3 checked you pay £10, for each additional selected checkbox another £1 each. I realise that I would need a loop somehow, but how can you count how many checkboxes have been ticked? Any help appreciated.Kurt

Link to comment
Share on other sites

Make your form somewhat like this:

  <form action="" method="post">	<p>	  <ul>		<li><input type="checkbox" name="hello[]" value="A1"> A1</li>		<li><input type="checkbox" name="hello[]" value="A2"> A2</li>		<li><input type="checkbox" name="hello[]" value="A3"> A3</li>		<li><input type="checkbox" name="hello[]" value="A4"> A4</li>		<li><input type="checkbox" name="hello[]" value="A5"> A5</li>		<li><input type="checkbox" name="hello[]" value="A6"> A6</li>	  </ul>	  <input type="submit" value="click me">	</p>  </form>

And then you'll be able to obtain all the values of the selected checkboxes in an array:

  <?php	if($_POST['hello']) {	  print_r($_POST['hello']);	}  ?>

Possible return value:

  Array(	[0] => A2	[1] => A3	[2] => A5)

Link to comment
Share on other sites

Make your form somewhat like this:
  <form action="" method="post">	<p>	  <ul>		<li><input type="checkbox" name="hello[]" value="A1"> A1</li>		<li><input type="checkbox" name="hello[]" value="A2"> A2</li>		<li><input type="checkbox" name="hello[]" value="A3"> A3</li>		<li><input type="checkbox" name="hello[]" value="A4"> A4</li>		<li><input type="checkbox" name="hello[]" value="A5"> A5</li>		<li><input type="checkbox" name="hello[]" value="A6"> A6</li>	  </ul>	  <input type="submit" value="click me">	</p>  </form>

And then you'll be able to obtain all the values of the selected checkboxes in an array:

  <?php	if($_POST['hello']) {	  print_r($_POST['hello']);	}  ?>

Possible return value:

  Array(	[0] => A2	[1] => A3	[2] => A5)

Ingolme,I need to make a note which checkbox has been selected. Therefore, I have each checkbox set up as <input type="checkbox" name="counties_42" id="counties_42" />with the number varying for each checkbox. Is there a different way to achieve what I am after?Kurt
Link to comment
Share on other sites

Not sure what you mean, but i think this would be right

	echo "<form method=\"post\">";	for($i=0;$i<50;$i++){ // make 50 checkboxes		echo "<input type=\"checkbox\" name=\"a_name[something_$i]\" />";	}	echo "<input type=\"submit\" />";	echo "</form>";	$checked	= array();//prevent warning if nothing is checked	foreach($_POST['a_name'] as $key=>$value){		$checked[]	= substr($key,10); //10 is the length of 'something_'	}		print_r($checked);

that will show which checkboxes you selectedthen for what u said, 3 costs 10, and each one costs 1 more could just do something like

$count	= count($checked);if($count<3){	// dont know what u want here}else{	$cost	= 10+($count-3);}

Link to comment
Share on other sites

Wander,You certainly made my day:-) This almost answers all my questions I have with this new task. Just one more: There are also two tickboxes named "discount". The first is for a 5% reduction of total and the other one for a 10% reduction. Is there a good way to incorporate the possibility that a user might opt for a reduction?In addition, just curious: When you started php and the whole lot would you have easily come up with sth like that? For me it is always very difficult to figure sth out if I have not done it before. I am just able to do things after I have seen how it works first (therefore I quite like tutorials and books).Kurt

Link to comment
Share on other sites

for the discount thing, add those 2 checkboxes for example as:<input type="checkbox" name="discount_1" /><input type="checkbox" name="discount_2" />then replace what i said at the end of a post above by something like:

$count	= count($checked);if($count<3){	// dont know what u want here}else{	$cost	= 10+($count-3);	if(isset($_POST['checked']['discount_1'])){ // the 5 percent discount is checked		$cost *= 0.95; // is same as $cost = ($cost/100)*95;	}	if(isset($_POST['checked']['discount_2'])){ // the 10 percent discount is checked		$cost *= 0.90;	}}

well, i cant really remember with what i used to came up when i just started, if i look now at things i wrote back then i see its all very crappy,i think to see things like this quick, is by practising a lot, not just by learning stuff from books, but by trying, and trying, and trying :)

Link to comment
Share on other sites

Wanda,Thanks for your help. Using your code with amended form: echo "<input type=\"radio\" name=\"checked\" value=\"discount_1\" id=\"discount_1\" />"; echo "<input type=\"radio\" name=\"checked\" value=\"discount_2\" id=\"discount_2\" />";it always calculates same price for both options. Also, when selecting 4 the total is 11 and a discount of 10% should make the total 9.9. The script brings it to 9.405. Where am I going wrong?KurtPS Thanks for letting me know that not everyone starts off as a genius;-)

Link to comment
Share on other sites

oh you want them as radio buttons?if the user can choose either 5% or 10%, give them the same name, different values, and check what vallue the $_POST['that_name'] hasfor example:<input type="radio" name="discount" value="0" /> , no discount<input type="radio" name="discount" value="1" /> , 5% discount<input type="radio" name="discount" value="2" />, 10% discountthen in the calculation part:

$cost	= 10+($count-3);if(isset($_POST['discount'])&&$_POST['discount']==1){ //5% discount	$cost *= 0.95;}elseif(isset($_POST['discount'])&&$_POST['discount']==2){ // 10%	$cost *= 0.9;}

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...