如果选中下拉菜单选项,则PHP / HTML更改文本

时间:2016-08-28 10:46:32

标签: php html

我正在像商店页面那样工作,您可以选择购买服务的时间。

有一个包含3个选项的下拉菜单:

  • 1 Week
  • 3 Weeks
  • 6 Weeks

我该怎么做:

如果点击选项3 Weeks,价格(简单文字)会自动更改为5 dollars6 Weeks10 Dollars或类似内容?

所有价格变动均由我完成。

我想知道如何通过从下拉菜单中选择内容将exampletext1更改为exampletext2

2 个答案:

答案 0 :(得分:4)

客户端

要在客户端事件上修改页面(例如:点击,选择更改)而不重新加载所有页面,您必须使用JavaScript之类的客户端语言。

服务器端

PHP仅用于服务器端。这意味着在将页面发送给用户之前执行PHP,并且无法在不向服务器发送新请求(并重新加载页面)的情况下捕获用户的操作。

以下是您提出的示例(使用javascript)

https://jsfiddle.net/9c1unscw/1/

<p>Select a period.</p>

<select id="mySelect" onchange="myFunction()">
  <option value="1">1 Week
  <option value="2">3 Weeks
  <option value="3">6 Weeks
</select>

<p id="demo">Estimated price: 5 dollars.</p>

<script type="text/javascript">
  function myFunction() {
      var x = document.getElementById("mySelect").value;
      var price = parseInt(x)*5;
      document.getElementById("demo").innerHTML = "Estimated price: " +price+" dollars.";
  }
</script>

答案 1 :(得分:3)

这是一个小算法,可以给你一个基本的想法:

<!DOCTYPE html>
<html>

<body>

  <p>Select a new car from the list.</p>

  <select id="mySelect" onchange="myFunction()">
    <option value="one">1 Week
      <option value="three">3 Week
        <option value="six">6 Week
  </select>

  <p id="price">Price: 1 dollars</p>

  <script>
    function myFunction() {
      /*
      You should get this price array from your PHP service dynamically
      whenever this page is requested.
      */
      var price = [];
      price['one'] = '1';
      price['three'] = '5';
      price['six'] = '10';


      var x = document.getElementById("mySelect").value;
      document.getElementById("price").innerHTML = "Price: " + price[x] + " dollars";

    }
  </script>

</body>

</html>