/    Sign up×
Community /Pin to ProfileBookmark

<option value> editing issue in form

hello, i have a script which updates a database. The only issue i have is that one of the variables , category, is set using the <input> tag, when i go to edit the value for category i can select a new option but the next time i go to edit the category it will automatically change back to the first <option> item on the list. How can i get the <option> to remain the same value as to the one in the database until i change it. Confussed? , its a tough one to explain i think

This is the code i am using at the moment:

[code=php]<Select name=”category” class=”box” id=”category” value=”<?=$category;?>”>
<option value=”Music”>Music</option>
<option value=”Waterford”>Waterford</option><option value=”Irish”>Irish</option></Select> [/code]

If i select irish and save, and then go to this page again the value will change back to music as it is the first one on the list. How can i bring up the chosen value until i edit it?

Thanks

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 06.2006 — [code=php]
<Select name="category" class="box" id="category" value="<?php echo $category; ?>">
<?php
foreach(array("Music", "Waterford", "Irish") as $value)
{
$selected = (isset($_POST['category'] and $value == $_POST['category']) ? " selected" : "";
echo "<option value='$value'$selected>$value</option>n";
}
?>
</select>
[/code]

(Change $_POST to $_GET if your form is using the get method.)
Copy linkTweet thisAlerts:
@acetoneMay 06.2006 — Query the database and get the value that is already set. Then add a line to the script after the select tag that reads:

<option value='$Music'>$Music - SELECTED</option>
Copy linkTweet thisAlerts:
@MegatronauthorMay 06.2006 — hi nogdog, i get a unexpected T_LOGICAL_AND, expecting ',' or ')' error on line 61 when i try to run the script Line 61 is

[code=php]$selected = (isset($_POST['category'] and $value == $_POST['category']) ? " selected" : "";[/code]
Copy linkTweet thisAlerts:
@NogDogMay 06.2006 — Missing paren:
<i>
</i> $selected = (isset($_POST['category'][color=red][b])[/b][/color] and $value == $_POST['category']) ? " selected" : "";
Copy linkTweet thisAlerts:
@MegatronauthorMay 06.2006 — that code doesn't work i'm afraid. The issue with the value in the db getting replaced by the first select option is still occuring.
Copy linkTweet thisAlerts:
@NogDogMay 06.2006 — Sorry, I didn't read the initial post closely enough. You'll have to query the DB first to get the current value, then use that instead of the $_POST value I used in my sample code.
Copy linkTweet thisAlerts:
@MegatronauthorMay 06.2006 — hi, on the page i have a query which returns all the stored values in the db using GET. So it should return the currently stored category. Here is the complete code:
[code=php]
<?
include 'db.php';

?>
<?php



if(isset($_GET['id']))
{
$query = "SELECT * FROM links WHERE id = '" . $_GET['id'] . "'";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_assoc($result);
extract($row);


}
else if(isset($_POST['sitename']))
{
$id = $_POST['id'];
$sitename = $_POST['sitename'];
$sitelink = $_POST['sitelink'];
$des = $_POST['des'];
$category = $_POST['category'];


$query = "UPDATE links SET sitename = '$sitename', sitelink = '$sitelink', des = '$des', category = '$category' WHERE id = '$id'";
mysql_query($query) or die('Error : ' . mysql_error());



echo "<p align='center'>Link updated</p>";

}

?>
<form method="post" action="edit.php">
<input type="hidden" name="id" value="<?=$id;?>">
<table width="700" border="0" cellpadding="2" cellspacing="1" class="box" align="center">
<tr>
<td width="100">Site Name</td>
<td><input name="sitename" type="text" class="box" id="sitename" value="<?=$sitename;?>" size="65"></td>
</tr>
<tr>
<td width="100">Site Link</td>
<td>
<input name="sitelink" type="text" class="box" id="sitelink" value="<?=$sitelink;?>" size="65">
</input></td>
</tr>
<tr>
<td>Description</td>
<td><input name="des" type="text" class="box" id="des" value="<?=$des;?>" size="65"></td>
</tr>
<tr>
<td>Category </td>
<td><Select name="category" class="box" id="category" value="<?php echo $category; ?>">
<?php
foreach(array("Music", "Waterford", "Irish") as $value)
{
$selected = (isset($_GET['category']) and $value == $_GET['category']) ? " selected" : "";
echo "<option value='$value'$selected>$value</option>n";
}
?>
</select> </td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="update" type="submit" class="box" id="update" value="Update Article">
</div></td>
</tr>
<tr>
<td colspan="2" align="center">&nbsp;</td>
</tr>
</table>

</form>
</tbody>
</table>

</body></html>[/code]
×

Success!

Help @Megatron 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 5.7,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...