help with php select options

  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

<select>
<option value="<?php $op='first'; ?>">Volvo</option>
<option value="<?php $op='second'; ?>">Saab</option>
</select>
<br />
<?php

if($op=='first'){
echo 'volvo';
}
else if($op=='second'){
echo 'saab';
}
else{
echo 'nothing';
}

?>

I don't know what I`m missing, the value won't change from first!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Nightslyr
  • Proficient
  • Proficient
  • Nightslyr
  • Posts: 283

Post 3+ Months Ago

Try:

Code: [ Select ]
<?php
  if (isset($_POST['submit']))
  {
   if ($_POST['op'] == "volvo") { echo "Volvo"; }
   else { echo "Saab"; }
  }
?>

<!-- other HTML, including the beginning of your form -->

<select name="op">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
</select>
<br />
<br />
<input name="submit" type="submit" value="submit" />

<!-- the rest of your HTML -->
  1. <?php
  2.   if (isset($_POST['submit']))
  3.   {
  4.    if ($_POST['op'] == "volvo") { echo "Volvo"; }
  5.    else { echo "Saab"; }
  6.   }
  7. ?>
  8. <!-- other HTML, including the beginning of your form -->
  9. <select name="op">
  10.   <option value="volvo">Volvo</option>
  11.   <option value="saab">Saab</option>
  12. </select>
  13. <br />
  14. <br />
  15. <input name="submit" type="submit" value="submit" />
  16. <!-- the rest of your HTML -->
  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

Thanks:)
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

If your select option value is the same as what you're wanting to print to the screen then the if/else statement is not needed and you can dynamically update your dropdown.

Code: [ Select ]


<?php
<?php
$op_select = array('Volvo','Saab');
 if (isset($_POST['submit']))
 {
$op_var = $_POST['op'];
echo $op_var;

//or update dropdown
$my_dropdown = '';
    foreach($op_select as $val) {
        if ($val == $op_var) {
        $my_dropdown .= '<option value="'.$val.'" selected>'.$val.'</option>';
        } else {
        $my_dropdown .= '<option value="'.$val.'">'.$val.'</option>';
        }
    }

 } else {
 // normal dropdown
 $my_dropdown = '';
    foreach($op_select as $val) {
        $my_dropdown .= '<option value="'.$val.'">'.$val.'</option>';
    }
 
 }
?>
<!-- other HTML, including the beginning of your form -->
<form action="dd.php" method="post">
<select name="op">
<?php echo $my_dropdown;?>
</select>
<input name="submit" type="submit" value="Submit"/>
</form>
<br />
  1. <?php
  2. <?php
  3. $op_select = array('Volvo','Saab');
  4.  if (isset($_POST['submit']))
  5.  {
  6. $op_var = $_POST['op'];
  7. echo $op_var;
  8. //or update dropdown
  9. $my_dropdown = '';
  10.     foreach($op_select as $val) {
  11.         if ($val == $op_var) {
  12.         $my_dropdown .= '<option value="'.$val.'" selected>'.$val.'</option>';
  13.         } else {
  14.         $my_dropdown .= '<option value="'.$val.'">'.$val.'</option>';
  15.         }
  16.     }
  17.  } else {
  18.  // normal dropdown
  19.  $my_dropdown = '';
  20.     foreach($op_select as $val) {
  21.         $my_dropdown .= '<option value="'.$val.'">'.$val.'</option>';
  22.     }
  23.  
  24.  }
  25. ?>
  26. <!-- other HTML, including the beginning of your form -->
  27. <form action="dd.php" method="post">
  28. <select name="op">
  29. <?php echo $my_dropdown;?>
  30. </select>
  31. <input name="submit" type="submit" value="Submit"/>
  32. </form>
  33. <br />
  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

that's going into some other direction.. I just needed a way to implement the variable into an editor that could read the page with the same name.. anyway.. thanks for the advice:)

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 162 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.