0

I have a simple problem in php select option.

The page has 5 select option & submit button. When I select a option then it should goes on specific web page. For Example: http://onlinetools.org/tricks/using_multiple_select.php

Then I select a option & press Send then It show Which option I select. But I need go specific webpage.

I have tried with this code but I have failed...

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
    <option value="http://google.com">one</option>
    <option value="http://yahoo.com">two</option>
    <option value="http://facebook.com">three</option>
    <option value="http://who.is">four</option>
    <option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>




<?php
    $test=$_POST['test'];
echo "
<script type=\"text/javascript\">
window.location = \"$test\"
</script>

    ";
?>

Anybody Can help me?

2
  • Please don't use $_SERVER['PHP_SELF']. Just leave the action empty. e.g. method="post" action="" Commented Jan 6, 2012 at 6:44
  • 2
    Why does your select box have multiple="multiple" when only one option at a time can make sense? Similarly, why have it be test[], when you're not treating it like an array? Commented Jan 6, 2012 at 6:46

2 Answers 2

5

This should be a simple select not a multiple one since you want to redirect to only one site. here is the code :

<?php
    $test=$_POST['test'];
    if(isset($test)){
        header("Location: $test");
    }
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test">
    <option value="http://google.com">one</option>
    <option value="http://yahoo.com">two</option>
    <option value="http://facebook.com">three</option>
    <option value="http://who.is">four</option>
    <option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

PHP_SELF is vulnerable, consider leaving it empty.
I left it untouched since I figured he is using this as a test script. but you are right, thx for the heads up.
0

As you've declared:

<select name="test[]" multiple="multiple">

test is an array, thus you have to take the first object of the array or remove the unused []. By the way, you can do an HTTP redirection, which is faster and works more often than javascript ones.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.