1

I am new to php and want to make a dropdown menu where you can select a site that you want, then when you click a button you will be redirected to that site. The button will take you to for example "google.com" if that is what you select, if you select "stackoverflow.com" the same button will take you there. I currently have no working php code as I am not sure where to start. I will include the html code below.

<form method="post" action="testttt.php">
<select id="mySelect" onchange="myFunction()">  
    <option value="SelectSite"> Select Site Please
    <option value="Itslearning"> Itslearning
    <option value="NDLA"> NDLA
</select>

    <input type="submit" value="GO"/>

The form ends a bit further down, so please don't complain about there being no :P ANY HELP IS APPRECIATED :)

7
  • 1
    Start by learning PHP, then write some PHP. This is not a difficult thing to achieve even with the basics. Commented Sep 2, 2016 at 8:29
  • i know some php, but i cant seem to find a way to accomplish this.. Commented Sep 2, 2016 at 8:30
  • header() will point you in the right direction. Commented Sep 2, 2016 at 8:31
  • thanks, ill be looking in to it :) Commented Sep 2, 2016 at 8:33
  • 1
    Doesn't even need PHP... or a button for that matter - JavaScript onchange > document.location would do it. Commented Sep 2, 2016 at 8:34

4 Answers 4

2

Try the below code , i have done the example which you want and its work perfectly . You can do it this in single file as i have done .

<?php
if(isset($_POST['btnsubmit']))
{

    $options = $_POST["testing"];
    header('Location: '.$options.'');
    echo "Your option value".$options;
}

?>
<form method="post" action="#">
<select id="mySelect" name="testing" onchange="myFunction()">  
    <option value="SelectSite"> Select Site Please
    <option value="http://www.google.com"> Itslearning
    <option value="http://www.stackoverflow.com"> NDLA
</select>

    <input type="submit" name="btnsubmit" value="GO"/>
</form>
Sign up to request clarification or add additional context in comments.

Comments

0
  • testttt.php will receive values in the $_POST array, so things like 'echo $_POST["somefield"];' will get you data...
  • ...BUT you have to name your form elements. Not '<select id="somefield"...' but '<select name="somefield"...'. you can still have id attributes if you want, but pass to $_POST happens by what you put in the name= attribute.
  • Once you know the URL you're redirecting to, header("Location: $url");

Hope that gives you the push you need :-)

1 Comment

this might explain why it didnt work when i tried using $_POST.... thanks for the input :)
0

can you not just use the myfunction function like this:

function myfunction(event){
    return window.open(event.target.options[event.target.options.selectedIndex].value,'');
}

Comments

0

If you want to do it in php, your select need a name attribute :

<form method="post" action="testttt.php">
    <select id="mySelect" name="mySelect" onchange="myFunction()">  
        <option value="SelectSite"> Select Site Please
        <option value="Itslearning"> Itslearning
        <option value="NDLA"> NDLA
    </select>

    <input type="submit" value="GO"/>
</form>

You can do that either in PHP :

if(isset($_POST['mySelect']) && !empty($_POST['mySelect'])){
    header('Location: ' . $_POST['mySelect']);
}

Or in Javascript using the onchange attribute you already set up :

myFunction(){
    var target = event.target;
    document.location.href(target.options[target.options.selectedIndex].value);
}

1 Comment

Thanks, i tried it with javascript at first, but i think ill be doing it in php as id like to learn more about it. Thanks for the input :9

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.