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

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型