首页 文章

提交时的HTML表单,根据所选的单选按钮值重定向到页面

提问于
浏览
1

我希望能够在表单上提交,根据所选的单选按钮值重定向到页面 .

我已经阅读了类似的解决方案,但我的特殊情况是我需要将输入值传递给URL .

例如:

(1)使用此URL在表单页面上开始(http://example.com/?cat=1

(2)选择单选按钮字段(值=“999”将放置“page123”或如果值= 888然后将“page456”放入URL)并单击提交(也“获取”“猫”隐藏的输入值)

(3)重定向到URL --->(http://example.com/page123/?cat=1

表单页面:

<form action="<MY DESIRED URL ON STEP 3>" method="get">
<input name="cat" type="hidden" />
<div class="row">
  <div class="third-row"><input id="1" class="input-hidden" name="styleid" type="radio" value="999" />
  <div class="third-row"><input id="2" class="input-hidden" name="styleid" type="radio" value="888" />
  <div class="third-row"><input id="3" class="input-hidden" name="styleid" type="radio" value="777" />
</div>
<input class="submit-button" type="submit" value="GO!" />
</form>

1 回答

  • 2

    只需使用switch语句,就可以根据输入值设置URL . 然后使用一个头语句重定向 .

    switch($_GET['styleid']){
        case "999":
            $url = "destinationURL";
            $params = "?cat='.$_GET['cat'].'";
            break;
        case "899":
            $url = "destinationURL";
            $params = "?cat='.$_GET['cat'].'";
            break;
        default:
            break;
    }
    if (isset($url) && isset($params)){
        header("location: ".$url.$params);
        die();
    }
    else{
        die('error');
    }
    

    编辑:要回答有关将cat值放入下一页的问题:

    在您的表单中修改您的隐藏输入:

    <input name="cat" type="hidden" value="<?php echo  trim(strip_tags($_GET['cat'])); ?>" />
    

    然后查看上面的编辑以设置参数 .

    编辑2:看到您的代码后,您将表单操作指向同一页面,并将处理器放在一个无效的标签内 .

    更新您的表单以指向您创建的新PHP文件 . 示例:位于名为“处理器”的文件夹中的“form-handler.php”

    <form action="/processors/form-handler.php" method="GET" target="self"></form>
    

    将switch语句和标头重定向放入处理器文件中 .

相关问题