duan1443 2017-11-26 13:26
浏览 61
已采纳

如何从表单中选择一个操作页面,然后将值提交到该页面HTML,PHP,Javascript

I'm trying to create a page with <form>, <option> and <select> attributes in a php page where I can select an action page from a dropdown menu. Like this Linking to other pages in HTML via drop-down menu And then depending on the option selected ask the user to input a text and be able to submit that reason to that action page. Similar to this Add input-box when selecting a specific option into select drop down

Also please post how you would submit the values to the action page in the way you have your code setup as currently I have been following this method. https://stackoverflow.com/a/1977439/9010400

Example of my form submission which doesn't have the action page selection.

If the below form action page is selected then post the values to that page, but I would like it to ask for a text input for the "reason" before posting.

<form action="url/page.php" method="POST"> <input type="hidden" name="user" value="tester" /> <input type="hidden" name="reason" value="test" /> <input type="submit" value="Ok"/>

In a way I'm trying to combine those different questions/answers I have mentioned in this post. I hope I have been clear as to what I trying to ask for help. Any help is much appreciated. Thank you.

Edit: Some more info: I want to able to post values to the form action I select. For example: Change form action on select option But I would also like to add a user input option and then submit that value as well to the action form.

  • 写回答

1条回答 默认 最新

  • douyi1855 2017-11-26 15:19
    关注

    Try this. It will look for the form which name matches the selected option value and show it. Every other form will be hidden.

    function showSelectedForm(){
      var selected_form = document.querySelector('select').value
      var forms = document.querySelectorAll('form')
      for(var i=0;i<forms.length;i++){
        if(forms[i].getAttribute('name')==selected_form)
          forms[i].style.display = 'block'
        else
          forms[i].style.display = 'none'
      }
    }
    form{
      margin-top: 25px;
      display: none;
    }
    form label{
      display: block;
    }
    Action:
    <select onchange="showSelectedForm()">
      <option disabled selected>Please choose</option>
      <option value="change_name">Change Name</option>
      <option value="report_user">Report User</option>
      <option value="delete_account">Delete Account</option>
    </select>
    
    <form name="change_name" action="/php/change_name.php">
      <label>Name <input type="text" name="name"></label>
      <label>Reason <input type="text" name="reason"></label>
      <input type="submit">
    </form>
    
    <form name="report_user" action="/php/report_user.php">
      <label>User <input type="text" name="user"></label>
      <label>Report <textarea name="report"></textarea></label>
      <input type="submit">
    </form>
    
    <form name="delete_account" action="/php/delete_account.php">
      <label>Name <input type="text" name="name"></label>
      <label>Are you sure? <input type="checkbox"></label>
      <input type="submit">
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?