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!

apply 'checked' to checkbox if $var = checkbox value

Status
Not open for further replies.

sophielois

Technical User
Sep 8, 2005
66
GB
Hi, ill try and explain the best i can.

i have 2 variables that are passed to a page which holds a form.

These are the two variables

Code:
$unit1 = H21
$unit2 = H22

and this is the form

Code:
<form action="add.php" method="post" onsubmit="return doSubmit();">
<input type="checkbox" name="cb[]" value="H21" onclick="keepTrack(this);" /> H21<br />
<input type="checkbox" name="cb[]" value="H22" onclick="keepTrack(this);" /> H22<br />
<input type="checkbox" name="cb[]" value="H23" onclick="keepTrack(this);" /> H23<br />
<input type="checkbox" name="cb[]" value="H24" onclick="keepTrack(this);" /> H24<br />
<input type="checkbox" name="cb[]" value="H25" onclick="keepTrack(this);" /> H25<br />
<input type="checkbox" name="cb[]" value="H26" onclick="keepTrack(this);" /> H26<br />
<input type="checkbox" name="cb[]" value="H27" onclick="keepTrack(this);" /> H27<br />
<input type="checkbox" name="cb[]" value="H28" onclick="keepTrack(this);" /> H28<br />
<input type="submit" name="add" value="Proceed">
</form>

I need to apply "checked" to the checkboxes that have the same value as $unit1 and $unit2

but I really dont know how i would do this
Sophx
 
ok got this figured now

Code:
<?php
$unit1 = 'H21';
$unit2 = 'H22';
?>
<form action="add.php" method="post" onsubmit="return doSubmit();">
<input type="checkbox" name="cb[]" value="H21" <?php if ($unit1 == 'H21' or $unit2 == 'H21') echo 'checked="checked"' ?>onclick="keepTrack(this);" /> H21<br />
<input type="checkbox" name="cb[]" value="H22" <?php if ($unit1 == 'H22' or $unit2 == 'H22') echo 'checked="checked"' ?>onclick="keepTrack(this);" /> H22<br />
<input type="checkbox" name="cb[]" value="H23" <?php if ($unit1 == 'H23' or $unit2 == 'H23') echo 'checked="checked"' ?>onclick="keepTrack(this);" /> H23<br />
<input type="checkbox" name="cb[]" value="H24" <?php if ($unit1 == 'H24' or $unit2 == 'H24') echo 'checked="checked"' ?>onclick="keepTrack(this);" /> H24<br />
<input type="checkbox" name="cb[]" value="H25" <?php if ($unit1 == 'H25' or $unit2 == 'H25') echo 'checked="checked"' ?>onclick="keepTrack(this);" /> H25<br />
<input type="checkbox" name="cb[]" value="H26" <?php if ($unit1 == 'H26' or $unit2 == 'H26') echo 'checked="checked"' ?>onclick="keepTrack(this);" /> H26<br />
<input type="checkbox" name="cb[]" value="H27" <?php if ($unit1 == 'H27' or $unit2 == 'H27') echo 'checked="checked"' ?>onclick="keepTrack(this);" /> H27<br />
<input type="checkbox" name="cb[]" value="H28" <?php if ($unit1 == 'H28' or $unit2 == 'H28') echo 'checked="checked"' ?>onclick="keepTrack(this);" /> H28<br />
<input type="submit" name="add" value="Proceed">
</form>
 
Sophie:

I'd recommend that you start looking at generating the HTML by string manipulation rather than using context switching mode where HTML is interspersed with PHP code.
Also, the above problem can easily be solved and put into a loop structure which would save you a lot of typing. The same logic can be applied to each line.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top