POST to different page depending on SELECTvalue?
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 f开发者_如何学运维rom 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!!
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.
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)
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.
i'd use javascript to change the target of the form
if you really want to use php, you should try curl:
精彩评论