dongquanyu5816 2016-06-08 21:04
浏览 43
已采纳

在php中通过post方法更改url

I'm trying to change the url path by submited form...

index.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
        <?php include 'file.php'; ?>
        <form action=<?php action(); ?> method="post">
            <fieldset>
                <legend>Choose ID by select tag</legend>
                <label>ID:</label>
                <select name="id" onchange="this.form.submit()">
                    <?php option(); ?>
                </select>
                <label><?php selcet(); ?></label>
            </fieldset>
        </form>
    </body>
</html>

file.php

<?php

function action() {
    $value = $_SERVER['PHP_SELF'];

    if (isset($_POST['id'])) {
        $value .= '?id=' . $_POST['id'];
    }

    echo $value;
}

function option() {
    $count = 3;

    for ($i = 1; $i <= $count; $i++) {
        $value = 1;

        if (isset($_POST['id'])) {
            $value = $_POST['id'];
        }

        if ($i == $value) {
            echo '<option value="' . $i . '" selected>' . $i . '</option>';
        }
        else {
            echo '<option value="' . $i . '">' . $i . '</option>';
        }
    }
}

function selcet() {
    $value = 1;

    if (isset($_POST['id'])) {
        $value = $_POST['id'];
    }

    echo 'Option ' . $value . ' selected.';
}

?>

the action() function not working as well, I'm getting wrong ids, I want to get ?id=2 while '2' selected.

Can someone help me please? Thanks! :)

  • 写回答

1条回答 默认 最新

  • dongzhi6463 2016-06-08 21:16
    关注

    To achieve your original request:

    [...] I want to get ?id=2 while '2' selected.

    You need to use GET not POST for the easiest and fastest results.

    index.php

    <?php include 'file.php'; ?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
        </head>
        <body>
            <form action="<?php echo active(); ?>" method="GET">
                <fieldset>
                    <legend>Choose ID by select tag</legend>
                    <label>ID:</label>
                    <select name="id" onchange="this.form.submit()">
                        <?php echo option(); ?>
                    </select>
                    <label><?php echo select(); ?></label>
                </fieldset>
            </form>
        </body>
    </html>
    

    file.php

    <?php
    function active() {
        $value = $_SERVER['PHP_SELF'];
        if (isset($_GET['id'])) {
            $value .= '?id=' . $_GET['id'];
        }
        return $value;
    }
    
    function option() {
        $count = 3;
        for ($i = 1; $i <= $count; $i++) {
            $value = 1;
            if (isset($_GET['id'])) {
                $value = $_GET['id'];
            }
            if ($i == $value) {
                echo '<option value="' . $i . '" selected>' . $i . '</option>';
            }
            else {
                echo '<option value="' . $i . '">' . $i . '</option>';
            }
        }
    }
    
    function select() {
        $value = 1;
        if (isset($_GET['id'])) {
            $value = $_GET['id'];
        }
        return 'Option ' . $value . ' selected.';
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问
  • ¥15 动态规划算法实现背包问题
  • ¥15 wpf程序使用过程中异常奔溃
  • ¥15 多目标优化算法在与其他算法数据对比结果判断
  • ¥15 CPTN和EAST,主干网络是VGG16,请问在ICDAR2015数据集上训练之后,CPTN和EAST模型的大小为多少
  • ¥15 按颜色进行点云分割-python
  • ¥15 Matlab如何实现汽车变道切入场景的批量仿真