0

I´m trying to get which option is in the databse from a <select>, here is my code:

<select name="category" value="<?php echo $category ?>" class="form-control">
    <option value="perros">Perro</option>
    <option value="gatos">Gato</option>
    <option value="peces">Pez</option>
    <option value="aves">Ave</option>
    <option value="reptiles">Reptil</option>
    <option value="roedores">Roedor</option>
    <option value="productos">Producto</option>
</select>

my PHP:

 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM posts WHERE ID=$id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);

 if($row)
 {

 $category = $row['category'];

the problem is that it shows the first <option> i have. Thank you

Whole code: http://pastebin.com/2jBjt3SD

8
  • Take out value="<?php echo $category ?>". This is open to SQL injections as well. Commented Dec 25, 2015 at 15:04
  • it´s a private page, so that wouldn´t be a problem Commented Dec 25, 2015 at 15:05
  • What is the relation between your PHP and form? Where does id come in? Also that could still be an issue, if your values ever contain a ' this won't work.. Commented Dec 25, 2015 at 15:05
  • The id comes from the filename, it fetches it with a _GET['']: if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) Commented Dec 25, 2015 at 15:09
  • Can you show more of the PHP code, not sure I'm following you? Does the PHP generate that HTML? Commented Dec 25, 2015 at 15:11

3 Answers 3

2

To set the selected option of a <select> you have to put the selected attribute on the correct option. Something like

<select name="category" class="form-control">
    <option <?php echo $category=='perros'?'selected':'' ?> value="perros">Perro</option>
    <option <?php echo $category=='gatos'?'selected':'' ?>value="gatos">Gato</option>
    <option <?php echo $category=='peces'?'selected':'' ?>value="peces">Pez</option>
    <option <?php echo $category=='aves'?'selected':'' ?>value="aves">Ave</option>
    <option <?php echo $category=='perros'?'selected':'' ?>value="reptiles">Reptil</option>
    <option <?php echo $category=='roedores'?'selected':'' ?>value="roedores">Roedor</option>
    <option <?php echo $category=='productos'?'selected':'' ?>value="productos">Producto</option>
</select>

you could use a loop to make it cleaner though

Sign up to request clarification or add additional context in comments.

Comments

0

I am afraid there's no shortcut for this. You need to set it like,

<select name="category" class="form-control">
    <option value="perros" <?php $category == 'perros' ? ' selected="selected"' : ''; ?>>Perro</option>
    <option value="gatos" <?php $category == 'gatos' ? ' selected="selected"' : ''; ?>>Gato</option>
    <option value="peces" <?php $category == 'peces' ? ' selected="selected"' : ''; ?>>Pez</option>
    <option value="aves" <?php $category == 'aves' ? ' selected="selected"' : ''; ?>>Ave</option>
    <option value="reptiles" <?php $category == 'reptiles' ? ' selected="selected"' : ''; ?>>Reptil</option>
    <option value="roedores" <?php $category == 'roedores' ? ' selected="selected"' : ''; ?>>Roedor</option>
    <option value="productos" <?php $category == 'productos' ? ' selected="selected"' : ''; ?>>Producto</option>
</select>

Or, if you use jQuery, you have an alternative approach like,

<script type="text/javascript">
    $(function(){
        var catVal = <?php echo $category; ?>;
        $('select[name=category]').val(catVal);
    });
</script>

Comments

0

Give the select and id and assign it a value with javascript like

<script type="text/javascript">
document.getElementById('your_id').vaue = <?php echo $category; ?>;
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.