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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记