douchuanhan8384 2018-04-27 19:52
浏览 105
已采纳

重置URL查询参数的表单帖子的逻辑?

I'm not getting the results expected when doing an update via onchange="formType.submit()" on a Select List.

I was hoping to have the PHP $_POST value set as a new URL but it looks like the value is not yet present when submitting the form.

Original URL: http://example.com/Select-item.php?type=4

<form action="Select-item.php?type=<?php echo $_POST['selItem']; ?>" method="post" id="formType">   
<select name="selItem" id="selItem" onchange="formType.submit()">
    <option value="1">change to 1</option>
    <option value="2">change to 2</option>
    <option value="3">change to 3</option>
</select>
</form>

The error I receive is undefined index (...Select-item.php?type=Notice:%20Undefined%20index:%20...)

Is the correct way to make this page URL update by posting to the same page, reading the $_POST value and then using

  • 写回答

2条回答 默认 最新

  • douqu2712 2018-04-27 20:03
    关注

    PHP generates the HTML code before it is transmitted to the client; after the client receiced the code, it will render it to a website.
    The reason your code is not working because when <?php echo $_POST['selItem']; ?> is executed there is no POST-Variable selItem set; it is set after the form was submitted.

    If you really want to transmit selItem as GET and as POST-Variable - a possible way of doing this is using this function (untested):

    <script>
        function onSelectChange() {
            // Access the select-field and get it's value
            var sel = document.getElementById("selItem");
            var selectedValue = sel.options[sel.selectedIndex].value;
    
            // Change the target URL of the form
            document.getElementById("formType").action = 'Select-item.php?type='+ selectedValue;
            formType.submit()
        }
    </script>
    

    And edit the onChange-Listener in your select: onchange="onSelectChange"

    BUT I would recommend using GET instead - this way selItem will be always in URL, too, and you don't need to check for $_GET and $_POST at the same time

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

报告相同问题?

悬赏问题

  • ¥15 openwrt双栈NAT
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用