Select checked on return

Asked

Viewed 226 times

1

I have the following code:

<p>Status</p>
<select id="st" name="st" value="<?php echo $DsStatus; ?>" />
    <option value="0">➖</option>
    <option value="1">➖⬆</option>
    <option value="2">➖⬇</option>
    <option value="3">↑➖</option>
    <option value="4">↓➖</option>
    <option value="5">↑⬇</option>
    <option value="6">↓⬆</option>
</select>

<input type="submit" class="round success button" value="Aplicar"/>

I need that after I click on Submit(apply) and return to the select page it is selected in the option I marked when I gave Submit(apply).

  • back to select page?

  • You are wanting to do an autorefresh with correct Submit?

  • I’m going to give him Ubmit, he’s going to leave this page, he’s going to go to another page, I just need that when I come back he takes the value that was saved. This page is a page that edits information so I need.

  • 2

    You can do with ajax or, check if the $_POST exist, and make an if if the value sent is equal to the option, and put the selected="selected"

  • It can be based on : http://answall.com/q/106370/91

  • 2

    simple, use $_SESSION

Show 1 more comment

2 answers

4


To do this, you need to return the selected value to the select page, suppose you return it in the variable $selecionado

$selecionado = $_REQUEST['st'];

On the page, you apply the following change to know who was selected:

<p>Status</p>
<select id="st" name="st" value="<?php echo $DsStatus; ?>" />
    <option value="0" <?= $selecionado == 0 ? 'selected' : ''; ?>>➖</option>
    <option value="1" <?= $selecionado == 1 ? 'selected' : ''; ?>>➖⬆</option>
    <option value="2" <?= $selecionado == 2 ? 'selected' : ''; ?>>➖⬇</option>
    <option value="3" <?= $selecionado == 3 ? 'selected' : ''; ?>>↑➖</option>
    <option value="4" <?= $selecionado == 4 ? 'selected' : ''; ?>>↓➖</option>
    <option value="5" <?= $selecionado == 5 ? 'selected' : ''; ?>>↑⬇</option>
    <option value="6" <?= $selecionado == 6 ? 'selected' : ''; ?>>↓⬆</option>
</select>

<input type="submit" class="round success button" value="Aplicar"/>

<?= $selecionado == 0 ? 'selected' : ''; ?> is a if ternário which means the same thing

if ($selecionado == 0) {
    echo 'selected';
} else {
    echo '';
}
  • 1

    It is right to use $_REQUEST?

  • @Tiagop. C Depending on the case, I used in the example because I didn’t know if the form was POST or GET, but the $_REQUEST together $_POST, $_GET and $_COOKIES.

  • The form is a $_POST, but I didn’t fit this $_REQUEST very well.

  • But that’s how it worked.

  • 1

    @Kevin. F can use $_POST['st'] in place then.

2

You can make two queries and make a comparison.

<?php
$sql="SELECT * FROM tabela WHERE id= $id";  
$resultado = $conexao->query($sql);
while($row = $resultado->fetch_assoc()) {

$st_selected = $row['st']
}

$sql="SELECT st FROM tabela  ORDER BY st";  
  $resultado= $conexao->query($sql);
while($row= $resultado->fetch_assoc()) {
   $st = $row['st'];

if($st  == $st_selected){
    $select='selected';
}else{
    $select=''; 
?>
<select id="st" name="st"  />
<?php
     echo '<option value="$st_selected" '.$select.'>$st_selected</option>';
    } 
?>

    <option value="0">➖</option>
    <option value="1">➖⬆</option>
    <option value="2">➖⬇</option>
    <option value="3">↑➖</option>
    <option value="4">↓➖</option>
    <option value="5">↑⬇</option>
    <option value="6">↓⬆</option>
</select>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.