/    Sign up×
Community /Pin to ProfileBookmark

selected option in php

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

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@Shanu_chaudharyApr 21.2008 — ? ?

Can u send the code??
Copy linkTweet thisAlerts:
@yona_tauthorApr 21.2008 — for example i have the insert form

[CODE]
<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>
[/CODE]


classes.php is something like
[CODE]
<?php
.....
$sql="insert into classes ....semester='$_POST["semester"]'";
$res=mysql_db_query();
?>
[/CODE]


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
[CODE]<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>
[/CODE]

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.
Copy linkTweet thisAlerts:
@Shanu_chaudharyApr 21.2008 — Ok...It means u r Loosing the selected option value on page refresh...right???
Copy linkTweet thisAlerts:
@yona_tauthorApr 21.2008 — 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.
[CODE]<input type="text" name="class_name" value=".stripslashes($result["code"]).">[/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?
×

Success!

Help @yona_t spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...