0

I want to have dropdown selected option selected after submit/refresh page. I search for other examples but no one works on my code.

How to retain selected value after submit/refresh with this code?

<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">   
    <select id="test_email" name="test_email">    
        <option value="">...select</option>
        <?php
            $sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
            $res = $db->query($sql2);

            if ($res->num_rows > 0) {
                while($row1 = mysqli_fetch_array($res)) {
        ?>
        <option value="<?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>"><?php echo $row1['test_id'];?></option>
        <?php 
                }
            }
        ?>
    </select>

Solved this way:

  • Because I could not pass $_POST['test_email'] with <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>
  • Because I use explode on $_POST['test_email']
  • I make one more post (insert in db) $test_temp = trim(mysqli_real_escape_string($db, $_POST['test_email'])); to have my dropdown value (whole string) in db.
  • I added hidden input in my form <input id="test_temp" type="hidden" name="test_temp" value="<?php echo $row["test_temp"]; ?>">
  • And changed select option line to <option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php if($row1['test_email'] . '-' . $row1['test_id'] == $row['test_temp']) echo ' selected="selected"' ; ?>><?php echo $row1['test_id'];?></option>.

Perhaps this could be simpler but it works like a charm.

@roberto06: Thank you for your guidance.

krlzlx
  • 5,752
  • 14
  • 47
  • 55
Falcon
  • 23
  • 6

1 Answers1

0

You need to add the selected="selected" attribute on the option matching your $_POST value.

You might also want to concatenate <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?> into <?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>, but that's up to you.

Here's a solution (I assumed that $POST['test_email'] has to be equal to $row1['test_email'] . '-' . $row1['test_id'] in order for the condition to be matched). I'm using a shorthand if...else here, but you could also use a classic if :

<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">   
    <select id="test_email" name="test_email">    
        <option value="">...select</option>
              <?php
                    $sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
                    $res = $db->query($sql2);
                    if ($res->num_rows > 0) {
                    while($row1 = mysqli_fetch_array($res)) {
                    ?>
        <option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php echo (isset($_POST['test_email']) && $row1['test_email'] . '-' . $row1['test_id'] == $_POST['test_email']) ? ' selected="selected"' : ''; ?>><?php echo $row1['test_id'];?></option>
                    <?php 
                       }
                      }
                     ?>
    </select>
</form>
Community
  • 1
  • 1
roberto06
  • 3,844
  • 1
  • 18
  • 29
  • Now, with your suggestion i have "Notice: Undefined index: test_email in C:\xampp...". Do I need `isset` POST and where pls? Or, problem is beacuse i use explode on test_email at test.php? Thanks anyway roberto06 – Falcon Nov 09 '16 at 16:18