Hi all,

This is going to seem a very basic sort of question so here goes....
i have a form with a couple of checkboxs and what i want to do is
echo the selected checkbox. for example;

if checkbox 1 is submitted then an image will be shown in a div underneath the form when it is submitted.
and if option 2 is selected then a differnt image will be shown and so on and so on.
all in the same page without redirecting and so on.

thanks in advance...

Cheers...

Recommended Answers

All 4 Replies

Hi all,

This is going to seem a very basic sort of question so here goes....
i have a form with a couple of checkboxs and what i want to do is
echo the selected checkbox. for example;

if checkbox 1 is submitted then an image will be shown in a div underneath the form when it is submitted.
and if option 2 is selected then a differnt image will be shown and so on and so on.
all in the same page without redirecting and so on.

thanks in advance...

Cheers...

This should work

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?
$checkboxes1 = array();
$checkboxes1[] = "value1";
$checkboxes1[] = "value2";
?>
<form action="<? echo $_SERVER['REQUEST_URI']; ?>" method="post">
<table cellpadding="0" cellspacing="0">
	<tr>
    	<td>Selected options below</td>
    </tr>
	<?
	foreach($checkboxes1 as $value)
	{
		$checkedcode = "";
		$checkeddisplay = "";
		if(isset($_POST['chkCheck']) && is_array($_POST['chkCheck']) && in_array($value, $_POST['chkCheck']))
		{
			$checkedcode .= '<tr>
                <td>include code here if the checkbox was selected</td>
            </tr>';
			$checkeddisplay = 'checked="checked" ';
		}
		?>
    	<tr>
        	<td><input type="checkbox" <? echo $checkeddisplay; ?>name="chkCheck[]" value="<? echo $value; ?>" /></td>
        </tr>
        <? echo $checkedcode; ?>
    <?
	}
	?>
</table>
</form>
</body>
</html>

Thanks Robbob, thats pretty much on the money. however...

firstly im an idiot, i wanted radio buttons not checkboxes. we wont even go there!

Secondly, could we have it where each radio button has a different outcome? so if 1 is selected img 1 shows if the second button is selected then img 2 is shown...


Thanks again

Cheers...

Thanks Robbob, thats pretty much on the money. however...

firstly im an idiot, i wanted radio buttons not checkboxes. we wont even go there!

Secondly, could we have it where each radio button has a different outcome? so if 1 is selected img 1 shows if the second button is selected then img 2 is shown...


Thanks again

Cheers...

This seems to work OK, just put your images in a folder under the document root called "rdo" and name your images to be the same as the value of the radio button, just replace the spaces with underscores.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?
$rdobuttons1 = array();
$rdobuttons1[] = "value1";
$rdobuttons1[] = "value2";
?>
<form action="<? echo $_SERVER['REQUEST_URI']; ?>" method="post">
<table cellpadding="0" cellspacing="0">
	<tr>
    	<td>Selected option below</td>
    </tr>
	<?
	foreach($rdobuttons1 as $value)
	{
		$checkedcode = "";
		$checkeddisplay = "";
		if(isset($_POST['rdoButton']) && $_POST['rdoButton'] == $value)
		{
			$checkedcode .= '<tr>
                <td>&nbsp;<img border="0" src="images/rdo/' . str_replace(" ", "_", $value) . '.jpg" /></td>
            </tr>';
			$checkeddisplay = 'checked="checked" ';
		}
		?>
    	<tr>
        	<td><input type="radio" id="<? echo str_replace(" ", "_", $value); ?>" <? echo $checkeddisplay; ?>name="rdoButton" value="<? echo $value; ?>" />&nbsp;<label for="<? echo str_replace(" ", "_", $value); ?>"><? echo ucwords($value); ?></label></td>
        </tr>
        <? echo $checkedcode; ?>
    <?
	}
	?>
    <tr>
    	<td><input type="submit" name="btnSubmit" value="Submit" /></td>
    </tr>
</table>
</form>
</body>
</html>

Cheers dude, thats magic.

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.