根据 SELECTvalue POST 到不同的页面?

发布于 2024-08-17 16:11:21 字数 244 浏览 9 评论 0原文

我有一个表单,具体取决于我想要发送到不同页面的

我不知道执行此操作的最佳方法,但我认为最好的方法可能是有一个中间页面始终发布到,然后让中间页面检查从

谢谢!!

I have a form, depending on the selected value of my <select> box I would like to send to a different page.

I do not know the best way to do this, but I am thinking that maybe the best way is to have an intermediate page to always post to, then have the intermediate page check the value sent from the <select> box and forward the POST values to a different page depending on what it is. If this is the best way, how can I do that? If not, what is the best way?

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

纵性 2024-08-24 16:11:21

这是一个好办法。另一种方法是使用 Javascript 拦截点击,重写表单的 action 属性并 submit() 它。

对于您的 PHP:

首页:

<form action="intermediary.php" method="post">
  <input id="submitTo">
    <select value="1">goToFirstPage</select>
    <select value="2">goToSecondPage</select>
  </input>
</form>

第二页:

<?php
  $url = "";
  if($_POST["submitTo"] == "1") $url = "http://mysite.com/firstPage.php";

 if($_POST["submitTo"] == "2") $url = "http://mysite.com/secondPage.php";

 header ("Location: $url");    
?>


Don't put actual URLs in your first page, or you'll run into massive security issues.

THat's a good way. The alternative is to use Javascript to intercept the click, rewrite the action property of the form and submit() it.

For your PHP though:

First page:

<form action="intermediary.php" method="post">
  <input id="submitTo">
    <select value="1">goToFirstPage</select>
    <select value="2">goToSecondPage</select>
  </input>
</form>

Second page:

<?php
  $url = "";
  if($_POST["submitTo"] == "1") $url = "http://mysite.com/firstPage.php";

 if($_POST["submitTo"] == "2") $url = "http://mysite.com/secondPage.php";

 header ("Location: $url");    
?>


Don't put actual URLs in your first page, or you'll run into massive security issues.

烟雨凡馨 2024-08-24 16:11:21

这当然是用户兼容性的最佳方法。

您可以使用 HTTP 重定向标头来完成此操作,例如,

header("Location: http://www.example.com/");

应在页面有任何输出之前调用该标头。

这样,当用户按下前进/后退按钮时,他甚至不会登陆到中间页面。

我看到的唯一其他选择是使用 javascript 来更改表单目标或这些行中的某些内容,但这有一些警告(必须启用 javascript,代码更复杂,重定向不应该真正发生在页面视图中)

that certainly would be the best method for user compatibility.

you could do it using the HTTP redirect header, e.g.

header("Location: http://www.example.com/");

which should be called before there is any output to the page.

That way the user won't even land on the intermediate page when he presses the forward/back buttons.

the only other option I see would be to use javascript to change the form target or something salong those lines, but that has some caveats (javascript has to be enabled, more complicated code, redirection shouldn't really happen in the view of a page)

枯寂 2024-08-24 16:11:21

然后

function form_submitted(){
   switch(document.myform.select_name.value){
     case 'whatever':
        document.myform.target.value = "whatever_you_need";
        break;
     case 'whatever2':
        document.myform.target.value = "whatever_you_need_2";
        break;
    }
}

,要么将此函数分配给具有“onSubmit”属性的表单,要么使用一个调用该函数的按钮(而不是提交按钮)并在函数末尾添加 document.myform.submit() 。

javascript

function form_submitted(){
   switch(document.myform.select_name.value){
     case 'whatever':
        document.myform.target.value = "whatever_you_need";
        break;
     case 'whatever2':
        document.myform.target.value = "whatever_you_need_2";
        break;
    }
}

Then either assign this function to the form with the "onSubmit" attribute, or have a button (rather than submit button) that calls that function and add document.myform.submit() at the end of the function.

蹲在坟头点根烟 2024-08-24 16:11:21

我会使用 javascript 来更改表单的目标,

如果您确实想使用 php,您应该尝试 curl :

i'd use javascript to change the target of the form

if you really want to use php, you should try curl:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文