Jump to content

Recommended Posts

A simplistic way to go about this would be:

 

<select>
<?php

for($i = 0; $i <= count($choice) - 1; $i++) {
echo "<option";
if($_POST["myChoice"] == $choice[$i]) {
echo " selected='selected'";
}
echo ">" . $_POST["myChoice"] . "</option>";
}
?>
</select>

 

Keep in mind that this is very simplistic, untested (I'm at work :B) and is probably not secure (unsanitized $_POST data just asking for an XSS Attack) but you get the idea. You create part of your HTML tag then add an if() statement. If the data in $_POST is equal to the choice being outputted, add the "selected='selected'" property to the tag. If the data doesn't match, it finishes off the HTML tag and carries on.

 

With some tweakage, you can use this to take information directly from a database and automatically select options based on row data. But be careful. If you don't sanitize user input, you leave yourself wide open to an XSS attack or much worse.

 

Hope this helps :)

Thanks to both of you, however I am having some problems getting my head around it, I have posted my code so you can see how I am getting my data from the database etc, The last block is the one I am working on (cat_show) Basically in the database is just 1 for yes or 0 for no:

 

<?php

$catid = $_GET['id'];

$query = "SELECT * FROM product_category WHERE catid = '$catid'";
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);

// while there are rows to be fetched...
while ($e = mysql_fetch_assoc($result)) {
?>
    
    <div id="body-container">
            <div id="body"><div id="container1"> 
<form action="formactions/updatecat.php" method="post" class="niceform"> 
<fieldset> 
    	<legend>Add Product Category</legend>
        <dl> 
        	<dt><label for="cat_name">Category Name:</label></dt> 
            <dd><input type="text" name="cat_name" id="cat_name" size="32" maxlength="32" value="<?php echo $e['cat_name'];?>" />
           </dd> 
            <script>
		var cat_name = new LiveValidation('cat_name');
		cat_name.add( Validate.Presence );
		</script>
        </dl>
        
        <dl> 
        	<dt><label for="cat_desc">Category Description:</label></dt> 
            <dd><textarea name="cat_desc" cols="32" rows="5" id="cat_desc"><?php echo $e['cat_desc'];?></textarea>
           </dd> 
            <script>
		var cat_desc = new LiveValidation('cat_desc');
		cat_desc.add( Validate.Presence );
		</script>
        </dl>
        
        <dt><label for="cat_show">Show to Customer?:</label></dt>
         <dd><select size="1" name="cat_show" id="cat_show"> 
                    <option value="1">Yes</option> 
                    <option value="2">No</option> 
                    </select>
                    </dd>
                    </dl>
         
         </fieldset>
         <fieldset class="action"> 
    	<input type="submit" name="submit" id="submit" value="Update Category" /> 
    </fieldset>
</form> 
<?php } ?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.