douzhouqin6223 2016-06-03 15:20
浏览 67
已采纳

保留php if语句中的select选项

I am attempting to create a single PHP page that opens a salesman or customer window based off a hidden input field in a form being posted. The tricky part for me is I also need two select option dropdowns that appear based on the same hidden input field being posted but ALSO retain their value after submit.

I know how to retain the values of a form like so..

<form action="" method="post">
    <select name="newyear">
        <option <?php if ($sqlyear == '2015cust') { ?>selected="true" <?php }; ?>value="2015cust">2015</option>
        <option <?php if ($sqlyear == '2016cust') { ?>selected="true" <?php }; ?>value="2016cust">2016</option>
    </select>
</form>

But when I try and implement this method inside my PHP if statements it does not retain the selected option.

if ($open == 'salesman_window'){
    echo "This is salesman dropdown menu";
    echo "$sqlyear";
    echo "<form action='' method='post'>";
    echo "<input type='hidden' name='newyear_trigger' value=''>";
    echo "<input type='hidden' name='window' value='salesman_window'>";
    echo "<select name='newyear'>";
    echo "<option if ($sqlyear == '2015users') { selected='true'  };  value='2015users'> 2015</option>";
    echo "<option if ($sqlyear == '2016users') { selected='true'  };  value='2016users'>2016</option>";
    echo "</select>";
    echo "<button class='documentation_button'>Submit</button>";
    echo "</form>";

}

if ($open == 'customer_window'){
    echo "this is customer dropdown menu";
    echo "$sqlyear";
    echo "<form action='' method='post'>";
    echo "<input type='hidden' name='newyear_trigger' value=''>";
    echo "<input type='hidden' name='window' value='customer_window'>";
    echo "<select name='newyear'>";
    echo "<option if ($sqlyear == '2015cust') { selected='true'  }; value='2015cust'>2015</option>";
    echo "<option if ($sqlyear == '2016cust') { selected='true'  }; value='2016cust'>2016</option>";
    echo "</select>";
    echo "<button class='documentation_button'>Submit</button>";
    echo "</form>";
}
?>

I have tried breaking up the option with multiple echo ""; and tried a few ".." of these in there as well but either get syntax errors or it just doesn't retain the selection. Everything works in regards to showing the correct dropdown menu based on what is posted but I just cannot get the select option to retain. The $sqlyear is for sure getting the correct value each time the dropdown option is selected so I know it isn't that either. Can anyone help?

  • 写回答

1条回答 默认 最新

  • dpw30157 2016-06-03 15:44
    关注

    A couple of options:

    1. Use a ternary operator in your echo:

      echo "<option" . (($sqlyear == '2015users') ? " selected='true'" : '') 
                 . " value='2015users'>2015</option>";
      echo "<option" . (($sqlyear == '2016users') ? " selected='true'" : '') 
                 . " value='2016users'>2016</option>";
      

    1. Keep using the if, but without opening/closing PHP tags as much:

      <option <?php if ($sqlyear == '2015users') echo " selected='true'"; ?> value='2015users'>
          2015</option>
      <option <?php if ($sqlyear == '2016users') echo " selected='true'"; ?> value='2016users'>
          2016</option>
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?