On May 6, 7:44 pm, dba <some...@someplace.orgwrote:
Have been displaying data from database using html for some time but
just recently trying to display data back to "form". Can't find answer.
<form method="post" action="<?php echo $PHP_SELF;?>">
First Name:<input type="text" size="12" maxlength="12" name="Fname"><br >
Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />
Gender:<br />
Male:<input type="radio" value="Male" name="gender"><br />
Female:<input type="radio" value="Female" name="gender"><br />
Now once I have the data in the database I want to display in the same
form. Have been doing googles and have for some time been displaying
using html like
echo "Hello, ".$Fname." ".$Lname.".<br />";
echo "You are ".$gender.", and you like ";
but just lately realized I have never display in a "form".
Any help will be appreciated.
Okay, I think I know what you are trying to do. You want to retrieve
data from a db to populate entry options on a form that was initially
used to enter the information. Here's sample code from one of my
scripts to give you a good example on how to do this. This is for the
select/options html element. You will have to do it differently for
text areas, radio boxes, and others. But this snippet should get you
on the right track:
//run the query to get all data stored in the INTAKE 1 FORM for this
id
$query = "select * from intake1 where consumer_id = ".$id;
$result = mysql_query($query);
if($row = mysql_fetch_array($result)) {
//there are many fields, but we are just using this one
//for demonstration purposes
$gender_form_entry = $row["gender_id"];
}
later on down the page i have this code for the gender section on the
form
<form>
....
<td>
<div class="formUnitVertical">
<label for="gender">Gender:</label>
<br/>
<select name="gender">
<?php
//get data entered in the gender table
$genderQuery = "select * from lst_gender";
$result = mysql_query($genderQuery);
//take resultset and makeform vars
while($row=mysql_fetch_array($result)) {
$gender_id = $row["id"];
$gender_type = $row["gender"];
//see if there is a gender id stored for
this form
//associated with this consumer id
if($gender_form_entry == $gender_id){
//yes, there is an id stored for this
form that matches
//an id from the gender table, so mark
it as SELECTED
echo "<option value=\"".$gender_id."\"
selected>".$gender_type."</option>";
}else{
//nothing stored in database, select
nothing
echo "<option value=\"".$gender_id."\">".
$gender_type."</option>";
}
}
?>
</select>
</div>
</td>
....
<input type='submit' />
...
</form>
hope this helps
chris