重置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

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

Always check using isset() function before using a $_POST

Example:

if(isset($_POST['selItem'])){
//your code here

}