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>
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据