dongxia527680 2015-05-02 19:16
浏览 7
已采纳

选择菜单,使用所选内容的变量

I'm fairly new to php, and coding in general.

I have a site where I will be having a horse trained. In the sql database I have a variable for how much they've been tamed, and when it's 100 it will stop. But the user can select from a drop down menu how long they'd like to tame the horse from 30 mins-4 hours. Each full hour is a full point towards the taming and a half point is worth .5. I want another variable that decreases the energy. For every 30 minutes it would decrease the energy by 5%. So I'd multiply whatever they chose by 10.

How do I get it to where it knows what the user selects and to then use that as what's updated in the value field?

This is the code I have:

<form id="Update" name="Update" method="POST" action="<?php echo   $editFormAction; ?>">
    <input type="submit" name="Tame" id="Tame" value="Tame" />
    <select name="Hours">
      <option value=".5">30 mins</option>
      <option value="1">1 hour</option>
      <option value="1.5">1 hour 30 mins</option>
      <option value="2">2 hours</option>
      <option value="2.5">2 hours 30 mins</option>
      <option value="3">3 hours</option>
      <option value="3.5">3 hours 30 mins</option>
      <option value="4">4 hours</option>
    </select>
    <input name = "Energy" type = "hidden" id = "EnergyDown" value = ""
    <input name="Date" type = "hidden" id = "Date" value= "<?php echo $RealDate ?>"/>
    <input type="hidden" name="MM_update" value="Update" />
    <input name="HorseId" type="hidden" id="HorseId" value="<?php echo $colname_HorseInfo ?> ">

</form>
  • 写回答

2条回答 默认 最新

  • douran6443 2015-05-02 19:44
    关注

    I'm assuming you want to update the value of the input with the name "Energy"?

    If so, you could do the following with javascript (just put it in the head part of the website, inside script tags.)

    Firstly set an ID & onchange in the , so:

    <select id="hours" name="Hours" onchange="setEnergy(this)">
    

    Overall the javascript will look like this:

    <head>
    <script>
    function setEnergy(x){
        //Get the value of the currently selected option
        var y = x[x.selectedIndex].value;
    
        //Multiply the value by 10
        var result = y * 10;
    
        //Change the value of the Energy input
        var energydown = document.getElementById("EnergyDown");
        energydown.value = result;
    }
    </script>
    </head>
    

    Also don't forget to set a default value. Your select tags will default to 30 mins, so the value should be set to 5 to account for this.

    If you change the input type from "hidden" to "text" when you test this, you'll be able to see it changing.

    Good luck, hope this helps!

    -Edit

    I was just out walking and realised that yeah, you could just submit it and work on it with PHP, but now you also know the javascript method; it will help you do live-updates on the client's page if necessary (which is what you asked for). :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决