开发者

selected option in select dissappears on reload

开发者 https://www.devze.com 2023-02-19 18:10 出处:网络
Once it loads in my page, if nothing has been saved in the DB table, all options are shown. As soon as i make a selection and reload the page, the selected option dissapears from the list and isn`t re

Once it loads in my page, if nothing has been saved in the DB table, all options are shown. As soon as i make a selection and reload the page, the selected option dissapears from the list and isn`t reloaded in the dropdown. Instead, it displays the next value which takes the place of the selected one.

if i check the SQL statement and the $str, it does load all the options except the one which is selected which is in $getBris (it has a value).

What could be causing my select to not display my selected option and instead removing it from the list?

*It specifically doesnt work in IE8, wasnt working in Firefox but now it does

<script src="validation.js" type="text/javascript"></script>

<html>
<body onLoad="checkSecondValue();">
</body>
</html>
<?php
    //retrieve all the bris for the drop down
include '../../inc/database.php';
// ORDER BY RAND()

$res = BbqcDatabase::getInstance()->doQuery('SELECT * FROM T_TOURNOI_BRIS');
$str = "<select name='ddlBrisSelected' id='ddlBrisSelected' onChange='checkSecondValue()'>";

$getBris = $_GET['bris'];
$getBris = $getBris - 1;
print_r("bris is : "+ $getBris);


if($getBris == null)
{
    $str .= "<option value='' selected></option>";
}
else
{
$str .= "<option value='999'>Choisir un bris</option>";
}

$i = 0;

while($data = mysql_fetch_assoc($res))
{
    if($data['F_BRISID'] == $getBris)
    {
        $str .= "<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>";
    }
    else 
    {
        $str .= "<option value='" . $data['F_BRISID'] . "'>" . $data['F_BRISTITLE'] . "</option>";
    }
    $i++;
}

if($getBris == 12)
{
    $str .= "<option v开发者_运维技巧alue=12 selected>Autre</option>";
}
else
{
    $str .= "<option value=12>Autre</option>";
}

$str .= "</select>";

echo $str;

if(is_numeric($bris))
{
    echo "<script type=\"text/javascript\">alert('test');checkSecondValue();</script>";
}
?>


Use your browser's View Source feature to inspect the actual HTML you are generating (which is, in fact, the only see the browser ever sees). It looks like you're inserting random single quotes.

Update:

<option value='" . $data['F_BRISID'] . "' selected '>" . $data['F_BRISTITLE'] . "</option>"

... will render as:

<option value='blah' selected '>blah</option>

It's the only error I've cared to spot but an HTML validator should find them all. Also, I recommend you use this syntax:

<option value="blah" selected="selected">blah</option>


A construct like this

if($getBris == 12)
{
    $str .= "<option value=12 selected>Autre</option>";
}
else
{
    $str .= "<option value=12>Autre</option>";
}

is highly wasteful of space and forces you to duplicate a big chunk of html whose only difference is the "selected" attribute. Why not do something like this:

$selected = ($getBris == 12) ? ' selected' : '';
$str .= "<option value=12{$selected}>Autre</option>";
0

精彩评论

暂无评论...
验证码 换一张
取 消