Jump to content

selecting allready picked value in a drop down menu


runnerjp

Recommended Posts

here is my code

 

<?php $lines = file('runningclubs.txt');
echo '<select class="inputedit" id="club" name="club">';
foreach($lines as $line) {
    echo '<option>'.$line.'</option>';
}
echo '</select>';?>

 

im struggling to figure out how to make it so that a value allready selected in a previus selection is shown

 

i know that <?php echo $pclub ?> will show my club value... but from here im stumped...

Okay, so you've got $pclub set to get the POST value from the previous form. Basically, just put an if statement in the foreach to check if $line == $pclub.

 

foreach($lines as $line) {
if($pclub == $line){
echo '<option selected="selected">'.$line.'</option>';
}else{
echo '<option>'.$line.'</option>';
}
}

ok i undertsand the consept you showed me but it doesnt work

 

<?php $lines = file('runningclubs.txt');
echo '<select class="inputedit" id="club" name="club">';
foreach($lines as $line) {
if($pclub == $line){
echo '<option selected="selected">'.$line.'</option>';
}else{
echo '<option>'.$line.'</option>';
}
}?>

 

this is what i have but it still fails to select the $pclub

You're missing the value of the option:

 

<?php $lines = file('runningclubs.txt');
echo '<select class="inputedit" id="club" name="club">';
foreach($lines as $line) {
if($pclub == $line){
echo '<option value="'.$line.'" selected="selected">'.$line.'</option>';
}else{
echo '<option value="'.$line.'">'.$line.'</option>';
}
}?>

Okay, well your closing the </select> right?

 

Try echoing out $pclub and $line on each cycle.

So:

foreach($lines as $line) {
echo "Line Val: {$line} - pclub: {$pclub}<br />";

 

And actually see that there is matching. It may be because your getting it from a file. But if you're originally setting the values with data from the same file then it wouldn't be that.

humm well my coee currently looks like

   <?php $lines = file('runningclubs.txt');
echo '<select class="inputedit" id="club" name="club">';
foreach($lines as $line) {
echo "Line Val: {$line} - pclub: {$pclub}<br />";
if($pclub == $line){
echo '<option value="'.$line.'" selected="selected">'.$line.'</option>';
}else{
echo '<option value="'.$line.'">'.$line.'</option>';
}
}?>

 

this line echo "Line Val: {$line} - pclub: {$pclub}<br />"; ecos out nothing...

 

and shaun0 im not sure what the trim() function is and how to use it

Haha of course it wouldn't. :P

 

It's inside a select tag. Anyway, this is what ShaunO meant.

<?php $lines = file('runningclubs.txt');
echo '<select class="inputedit" id="club" name="club">';
foreach($lines as $line) {
if($pclub == trim($line)){
echo '<option value="'.$line.'" selected="selected">'.$line.'</option>';
}else{
echo '<option value="'.$line.'">'.$line.'</option>';
}
}
echo "</select>";
?>

 

Remember, the PHP Manual is a good resource.

trim()

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.