Click to See Complete Forum and Search --> : selected option in php
yona_t
04-21-2008, 05:48 AM
Hi I am having this form for inserting some content. The form contains a select element. what I want to do is to have a similar form with the inserting one for updating the content. My problem is that when I retrieve data from the database and load it in the edit form I want the select element to have the right option selected. e.g. if i have chosen to color red when i inserted the data to have the color red selected again so that when i click update without changing the color it wont lose the value it had initially. How can that be done???
Thnx in advance
Yona T
Shanu.chaudhary
04-21-2008, 05:53 AM
:confused: :confused:
Can u send the code??
yona_t
04-21-2008, 06:09 AM
for example i have the insert form
<form method="post" action="classes.php">
<select name=semester>
<option value=''></option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
<submit button></form>
classes.php is something like
<?php
.....
$sql="insert into classes ....semester='$_POST["semester"]'";
$res=mysql_db_query();
?>
when I click on update class I want to run an sql query and retrieve all data from database that are related to that class and preview the class' edit form as follows
<form action=\"classes.php\" method=\"post\" onsubmit=\"return validate_class();\">
<input type="hidden" name="class_id" value=".$result["id"]." />
<table >
<tr>
<td>
Class name (*) </td>
<td>
<input type="text" name="class_name" value=".stripslashes($result["name"])."></td>
</tr>
<tr>
<td>
Class code (*) </td>
<td>
<input type="text" name="class_name" value=".stripslashes($result["code"])."></td>
</tr>
<tr>
<td>
Semester (*) </td>
<td>
<select name=semester>
<option value=''></option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
</select>
</td>
</tr>
</table><submit button></form>
but I want the right semester to be selected in the edit form when I load it. When inserted the class i selected that the class belongs to the 2nd semester. When I click on update class I want the select element to have the right semester selected and not to be blank and waiting for me to choose semester again.
Shanu.chaudhary
04-21-2008, 06:15 AM
Ok...It means u r Loosing the selected option value on page refresh...right??:)
yona_t
04-21-2008, 06:23 AM
no I mean that on one hand i have the insert form for inserting the data related to the class and on the other hand I have the update class form where I want to load all the data that i have stored in the database for a particular class. the problem is that when i chose to update a particular lesson free text data such as name, code etc can be loaded in the update form simply by using the value parameter of the input element.
<input type="text" name="class_name" value=".stripslashes($result["code"]).">
but on the select element i have no such parameter (as far as i know) for having the right option selected.
how can i do that?