php - Option value selected for loop -
i trying create selected
value of previous output. (the user fills in form , submits, returns form) , want selected value value user used.
the vraagnummer
, answer
given in url parameter , dropdown menu of items available in list created in loop. got stuck on part.. how create option selected value if created in loop?
usually put in option value = $vraagnummer selected> <?php echo $vraagnummer ?></option
in case wouldn't work. suppose?
anyone knows how fix this?
same a/b/c/d. how put in selected value of answer
value selected while still keeping others option.
kind regards,
if(!empty($_get['vraagnummer'])){ $vraagnummer = $_get['vraagnummer']; if(!empty($_get['answer'])){ $answer = $_get['answer']; } } echo $vraagnummer; echo $answer; ?> <form id="start" method="post" action="index.php?test=true"> <select name="question"> <?php for($i= 1; $i < $nrows+1 ; $i++){ ?><option value="<?php echo $i ?>">question <?php echo $i?></option> <?php } ?> </select> <select name="result"> <option value="a">answer a</option> <option value="b">answer b</option> <option value="c">answer c</option> <option value="d">answer d</option> <input type="submit" id="submit" value="submit" /> </form>
you test $_get['question']
against current $i
value in loop using ternary if
<form id="start" method="post" action="index.php?test=true"> <select name="question"> <?php for($i= 1; $i < $nrows+1 ; $i++){ $sel = (isset($_get['question']) && $_get['question'] == $i) ? 'selected="selected"' : ''; echo '<option ' . $sel . ' value="' . $i .">question ' . $i . '</option>'; } ?> </select>
warning:
also careful use of
empty()
when variables can contain 0 zero considered emptyempty()
function
actually other dropdown this
<select name="result"> foreach (range('a', 'd') $char) { $sel = (isset($_get['result']) && $char == $_get['result']) ? 'selected="selected"' : ''; echo "<option $sel value='$char'>answer $char</option>"; } </select>
Comments
Post a Comment