I have serious contempt for checkboxes no matter what I try they never seem 2 display even though everythin else is fine. I have 2 files. 1 is an html file where I put my checkboxes and is linked to the php file. The other ofcourse is a php file. Now it amounts to the html is where the data is inserted and the php file is the output.

html file code for checkbox

<p class="choice">
Please choose the confectionary you would like to buy
<br/>
liquorice:
<input type="checkbox" name="ch1" value="choc" <?PHP print $ch1; ?>/>
<br />
chocolate:
<input type="checkbox" name="ch2" value="liqu"<?PHP print $ch2; ?> />
<br />
lollipops:
<input type="checkbox" name="ch3" value="wine"<?PHP print $ch3; ?> />
<br/>
fruitgums:
<input type="checkbox" name="ch4" value="lolli"<?PHP print $ch4; ?> />
<br/>
<?php
$ch1 = 'unchecked';
$ch2 = 'unchecked';
$ch3 = 'unchecked';
$ch4 = 'unchecked';

if (isset($_POST)) {

if (isset($_POST)) {
$ch1 = $_POST;

if ($ch1 == 'choc') {
$ch1 = 'checked';
}
}

if (isset($_POST)) {
$ch2 = $_POST;

if ($ch2 == 'liqu') {
$ch2 = 'checked';
}
}

if (isset($_POST)) {
$ch3 = $_POST;

if ($ch3 == 'wine') {
$ch3 = 'checked';
}
}

if (isset($_POST)) {
$ch4 = $_POST;

if ($ch4 == 'lolli') {
$ch4 = 'checked';
}
}
}
?>
</p>

php file code for checkboxes

echo $ch1;
echo $ch2;
echo $ch3;
echo $ch4;
?>

All I need it to do is display the checkboxes selected/unselected when I submit the form. Can anyone help with this?
Just a quick note my attitude towards code is that I do everything I can do like showing addresses, submit buttons etc and then something I cant do I try everything I can come up with and research and if I still cant do it I post it. I also will never put whole assignment things on here because I would get kicked off my uni course if I did.

Recommended Answers

All 3 Replies

For one, you don't show any <form> or submit button. Secondly, your php is setting the variables after your HTML has been processed, so the variables in there have no value yet. The following revision works

<?php
$ch1 = 'unchecked';
$ch2 = 'unchecked';
$ch3 = 'unchecked';
$ch4 = 'unchecked';

if (isset($_POST['submit'])) {

if (isset($_POST['ch1'])) {
$ch1 = $_POST['ch1'];

if ($ch1 == 'choc') {
$ch1 = 'checked';
}
}

if (isset($_POST['ch2'])) {
$ch2 = $_POST['ch2'];

if ($ch2 == 'liqu') {
$ch2 = 'checked';
}
}

if (isset($_POST['ch3'])) {
$ch3 = $_POST['ch3'];

if ($ch3 == 'wine') {
$ch3 = 'checked';
}
}

if (isset($_POST['ch4'])) {
$ch4 = $_POST['ch4'];

if ($ch4 == 'lolli') {
$ch4 = 'checked';
}
}
}
?>
<form method='post' action='<?php $_SERVER['PHP_SELF'] ?>'>
<p class="choice">
Please choose the confectionary you would like to buy
<br/>
liquorice:
<input type="checkbox" name="ch1" value="choc" <?PHP print $ch1; ?>/>
<br />
chocolate:
<input type="checkbox" name="ch2" value="liqu" <?PHP print $ch2; ?> />
<br />
lollipops:
<input type="checkbox" name="ch3" value="wine" <?PHP print $ch3; ?> />
<br/>
fruitgums:
<input type="checkbox" name="ch4" value="lolli" <?PHP print $ch4; ?> />
<br/>
<input type='submit' name='submit' value='submit'/>
</form>

</p>

(I didn't bother to put indents back in since you didn't use code tags. If you use code tags your formatting will be retained and it will be much easier to read.)

You need to wrap this in a form tag, and then submit it with a <input type="submit">.

Here's how the form is supposed to look, taken that this is the file index.php:

<form action="./index.php" action="POST">
<input type="checkbox" name="ch1" value="checked" /> Choc<br />
<input type="checkbox" name="ch2" value="checked" /> Liqu<br />
<input type="checkbox" name="ch3" value="checked" /> Wine<br />
<input type="checkbox" name="ch4" value="checked" /> Lolli<br />
<input type="submit" value="Test this lil' form"/>
</form>

If a value box was checked the $_POST[name] will be value, so if Choc is checked, $_POST['ch1'] = "checked"; . On the other hand, if the check box was not set, no value will be send. So $_POST['ch1'] won't even be set at all.

Why did you wrap a if(isset($_POST['Sub'])) around it? since there is nothing that will set sup the code under it will never be executed. That might to be your problem.

Good luck with your form, I know I'm not that good at explaining stuff, so feel free to ask if something is not clear.

thnx a lot I am gonna give u a rep point for that if I can work out how to do it. I am still finding my feet a bit with php and orders is the thing I have most issue with. I have got used to using jcreator and stuff like visual basic using notepad has been a bit of a different thing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.