Page 1 of 1

Showing values on html pages

Posted: Fri Apr 04, 2003 5:01 am
by Shantanu
Dear All,

I am facing the foll. problem with HTML-PHP.
I have a HTML form on which I have certain controls (say radio buttons),
I have to fetch data from PostGRESQL database and according to the data I have to set the state of the radio buttons. i.e. checked or unchecked.

I am a newbie to PHP. So requesting for help.........

Thanx in adv.........

Shantanu

Posted: Fri Apr 04, 2003 5:25 am
by volka
which part of the script troubles you?

maybe this example of shipping form-data is a beginning.

Code: Select all

<html><body>
<pre><?php print_r($_POST); ?></pre>
<table border="1">
	<tr>
		<td>chk1</td>
		<td>
			<?php echo (isset($_POST['chk1'])) ? $_POST['chk1'] : 'not set'; ?>
		</td>
	</tr>
		<tr>
		<td>chk2</td>
		<td>
			<?php echo (isset($_POST['chk2'])) ? $_POST['chk2'] : 'not set'; ?>
		</td>
	</tr>
		<tr>
		<td>chk3</td>
		<td>
			<?php echo (isset($_POST['chk3'])) ? $_POST['chk3'] : 'not set'; ?>
		</td>
	</tr>
	<tr>
		<td>chkArray</td>
		<td>
			<?php
				if (isset($_POST['chkArray']))
				{
					foreach($_POST['chkArray'] as $key=>$value)
						echo $key, '=>', $value, '<br />';
				}
				else
					echo 'not set';
			?>
		</td>	
	</tr>
	<tr>
		<td>radio1</td>
		<td>
			<?php echo (isset($_POST['radio1'])) ? $_POST['radio1'] : 'not set'; ?>
		</td>
	</tr>
	<tr>
		<td>radio2</td>
		<td>
			<?php echo (isset($_POST['radio2'])) ? $_POST['radio2'] : 'not set'; ?>
		</td>
	</tr>
</table>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	<p>
		<input type="checkbox" name="chk1" id="chk1" /><label for="chk1">check #1</label> <br />
		<input type="checkbox" name="chk2" id="chk2" /><label for="chk2">check #2</label> <br />
		<input type="checkbox" name="chk3" id="chk3" /><label for="chk3">check #3</label>
	</p>
	
	<p>
		<input type="checkbox" name="chkArray[]" value="1" id="chka1" /><label for="chka1">check #1</label> <br />
		<input type="checkbox" name="chkArray[]" value="2" id="chka2" /><label for="chka2">check #2</label> <br />
		<input type="checkbox" name="chkArray[]" value="3" id="chka3" /><label for="chka3">check #3</label>
	</p>
	
	<p>
		radio1: (this one's not very usefull, take a look at the results)
		1<input type="radio" name="radio1" /> 
		2<input type="radio" name="radio1" /> 
		3<input type="radio" name="radio1" /> <br />
		<br/>
		radio2:
		1<input type="radio" name="radio2" value="1" /> 
		2<input type="radio" name="radio2" value="2" /> 
		3<input type="radio" name="radio2" value="3" /> <br />
	</p>
	<input type="submit" />
</form>
</body></html>

Posted: Fri Apr 04, 2003 5:51 am
by Shantanu
Thanx for the help........
this code i think will print some value in the column next to the checkboxes.
What I am interested in is the state of the checkboxes/radio buttons itself.

e.g. If a field in database table has value "1", I have to show the checkbox checked else it is unchecked....


Thanx agian in adv.........


Shantanu

Posted: Fri Apr 04, 2003 6:26 am
by volka
ic, then some other examples
first a simple static html

Code: Select all

&lt;html&gt;
	&lt;body&gt;
		&lt;form method="POST" action="&lt;?php echo $_SERVER&#1111;'PHP_SELF']; ?&gt;" &gt;
			1&lt;input type="checkbox" name="chk1" /&gt;
			2&lt;input type="checkbox" name="chk2" checked="checked" /&gt;
			3&lt;input type="checkbox" name="chk3"  /&gt;
			&lt;br /&gt;
			1&lt;input type="radio" name="radio1" value="1" /&gt;
			2&lt;input type="radio" name="radio1" value="2" /&gt;
			3&lt;input type="radio" name="radio1" value="3" checked="checked" /&gt;
			&lt;br /&gt;
			&lt;select name="sel1"&gt;
				&lt;option value="1"&gt;1&lt;/option&gt;
				&lt;option value="2" selected="selected"&gt;2&lt;/option&gt;
				&lt;option value="3"&gt;3&lt;/option&gt;
			&lt;/select&gt;
			&lt;br /&gt;
			&lt;select name="sel2" multiple="multiple"&gt;
				&lt;option value="1" selected="selected"&gt;1&lt;/option&gt;
				&lt;option value="2" &gt;2&lt;/option&gt;
				&lt;option value="3" selected="selected"&gt;3&lt;/option&gt;
			&lt;/select&gt;
			&lt;br /&gt;
			&lt;input type="submit" /&gt;
		&lt;/form&gt;
&lt;/body&gt;&lt;/html&gt;
and now dynamic

Code: Select all

<html>
	<body>
<?php
/** this is a dummy-result 
how how this data is retrieved from the database and what the values are and mean is up to you
just an example 
*/
$row = array(
	'chk1' => 'On',
	'chk2' => 'Off',
	'chk3' => 'On',
	'radio1' => 2,
	'radio2' => 0
);
?>
		<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			<input type="checkbox" name="chk1" <?php if ($row['chk1']=='On') echo 'checked="checked"'; ?> />
			<input type="checkbox" name="chk2" <?php if ($row['chk2']=='On') echo 'checked="checked"'; ?> />
			<input type="checkbox" name="chk3" <?php if ($row['chk3']=='On') echo 'checked="checked"'; ?> />
			<br />
			<input type="radio" name="radio1" value="1" <?php if($row['radio1']==1) echo 'checked="checked"'; ?> />
			<input type="radio" name="radio1" value="2" <?php if($row['radio1']==2) echo 'checked="checked"'; ?> />
			<input type="radio" name="radio1" value="3" <?php if($row['radio1']==3) echo 'checked="checked"'; ?> />
			<br />
			<input type="radio" name="radio2" value="1" <?php if($row['radio2']==1) echo 'checked="checked"'; ?> />
			<input type="radio" name="radio2" value="2" <?php if($row['radio2']==2) echo 'checked="checked"'; ?> />
			<input type="radio" name="radio2" value="3" <?php if($row['radio2']==3) echo 'checked="checked"'; ?> />
			<br />
			<input type="submit" />
		</form>
</body></html>

Posted: Fri Apr 04, 2003 6:55 am
by Shantanu
thanx volka,
it worked fine!!!!!

Posted: Fri Apr 04, 2003 7:05 am
by m3mn0n
Yea volka sure is a miracle worker, eh? :)