0

I need help with selected row after get method... I have table in mysql where fill dropdown rows and I really dont know how to put option value with $_get method... If user want to change some fields(edit it), previous row which is selected and saved needs to be selected... This is code:

<label for="" style="width:100px" id="a1" >Tarifa: </label>

<?php 
$result = $db->query("select sifra, porez from porezi");
echo "<select onchange='dropdownn();' style='width:100px' id='tarifa' name='tarifa_porez' class='input-sm form-control'>";

while ($row = $result->fetch_assoc()) {

    unset($sifra, $porez);
    $sifra = $row['sifra'];
    $porez = $row['porez']; 
    echo '<option value="'.$porez.'">    '.$porez.' %</option>';
}
echo "</select>";
?>

So, this is select, but I need help to read from table which one value is selected and select it on edit link (with $_get).

Thanks guys !

4
  • 1
    Welcome to SO. Please read What topics can I ask about and How to ask a good question And the perfect question And how to create a Minimal, Complete and Verifiable example SO is not a free Coding or Code Conversion or Debugging or Tutorial or Library Finding service Here at SO we fix your attempts, we do not code things for you Commented Jan 3, 2017 at 12:18
  • $_GET['tarifa_porez'] fetch using given code Commented Jan 3, 2017 at 12:19
  • @NishantNair but still not selecting Commented Jan 3, 2017 at 12:21
  • 1
    Check if the dropdown is in form or not Commented Jan 3, 2017 at 12:25

1 Answer 1

1

All you have to do is use selected attribute of <option>

So, your modified code:

//.. Your code...
$tarifa_porez = ! empty($_GET['tarifa_porez']) ? $_GET['tarifa_porez'] : null;
while ($row = $result->fetch_assoc()) {
    unset($sifra, $porez);
    $sifra = $row['sifra'];
    $porez = $row['porez'];
    $selected = ($tarifa_porez == $porez) ? 'selected="selected"' : '';
    echo '<option value="'.$porez.'" ' . $selected . '>    '.$porez.' %</option>';
}
echo "</select>";
?>

Note: I have used ternary operator to reduce number of lines. Please read it here.

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

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.