简体   繁体   中英

Form Auto select to specific link subdomain

I am trying to make it so when you select an option, it goes to a specific subdomain. eg if coffee is selected, it should redirect to coffee.domain.com. This is what I have, it seems to work but doesn't go to a chosen subdomain

<form>
<select name='http://domain.com' onchange='this.form.submit()'>
  <option selected>Select Option</option>
  <option value="coffee">Coffee</option>
  <option value="tea">Tea</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

The code you posted is just HTML. There is no code there to actually do what you want.

If you wanted to do this in PHP you could try something like

<?php
    if(ISSET($_POST['domainSelect']) == 'coffee'){
        header('Location: http://www.domain-coffee.com');
    }
    if(ISSET($_POST['domainSelect']) == 'tea'){
        header('Location: http://www.domain-tea.com');
    }
?>

<form method="POST">
<select name='domainSelect' onchange='this.form.submit()'>
  <option selected>Select Option</option>
  <option value="coffee">Coffee</option>
  <option value="tea">Tea</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

Or you could do it with JavaScript by doing something like

<script type='text/javascript'>
    function gotodomain(){
        var e = document.getElementById('domainSelectId');
        var strSelected = e.options[e.selectedIndex].value;

        if(strSelected == 'coffee'){
            document.location.href = 'http://domain-coffee.com'
        }

        if(strSelected == 'tea'){
            document.location.href = 'http://domain-tea.com'
        }
    }
</script>

<form method="POST">
<select id="domainSelectId" name='domainSelect' onchange='gotodomain()'>
  <option selected>Select Option</option>
  <option value="coffee">Coffee</option>
  <option value="tea">Tea</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

In either case I suggest you start thinking about what it is exactly you want and in what language its best to achieve in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM