最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Redirect with HTML dropdown select - Stack Overflow

programmeradmin8浏览0评论

I have a simple HTML drop-down select box.

I would like this to function as 'once something is selected' > the page then redirects to a custom URL.

Below is my mark-up.

<fieldset id="size"><legend>Product Options</legend>
<div class="wpsc_variation_forms">
<table>
<tr><td class="col1"><label for="variation_select_48_5">Choose Size:</label></td>
<td class="col2"><select class="wpsc_select_variation" name="variation[5]" id="variation_select_48_5">
<option value="0" >-- Please Select --</option>
<option value="8" >Large</option>
<option value="7" >Medium</option>
<option value="6" >Small</option>
</select></td></tr>
</table>
</div><!--close wpsc_variation_forms-->
</fieldset>

I found a similar question; but the solution given seems a bit clunky.

SELECT Dropdown that redirects without Javascript

I have a simple HTML drop-down select box.

I would like this to function as 'once something is selected' > the page then redirects to a custom URL.

Below is my mark-up.

<fieldset id="size"><legend>Product Options</legend>
<div class="wpsc_variation_forms">
<table>
<tr><td class="col1"><label for="variation_select_48_5">Choose Size:</label></td>
<td class="col2"><select class="wpsc_select_variation" name="variation[5]" id="variation_select_48_5">
<option value="0" >-- Please Select --</option>
<option value="8" >Large</option>
<option value="7" >Medium</option>
<option value="6" >Small</option>
</select></td></tr>
</table>
</div><!--close wpsc_variation_forms-->
</fieldset>

I found a similar question; but the solution given seems a bit clunky.

SELECT Dropdown that redirects without Javascript

Share Improve this question edited May 23, 2017 at 12:21 CommunityBot 11 silver badge asked Aug 26, 2013 at 21:26 Lieutenant DanLieutenant Dan 8,29828 gold badges97 silver badges216 bronze badges 1
  • where does the custom url e from? – Rooster Commented Aug 26, 2013 at 21:30
Add a ment  | 

4 Answers 4

Reset to default 3

With jQuery:

$(function() {
    $('#variation_select_48_5').change(function() {
        document.location = 'http://customurl.abc';
    });
});

The easiest way i know of is assigning an onchange event to select element and making options' values as urls.

<select onchange="window.location = this.options[this.selectedIndex].value;">
<option value="http://your-url">Large</option>
</select>

I would write i litte function and then trigger it depending on the retuned result

<?PHP
    function redirect($where){      
       header("Location: $where");
    }

    if ($_REQUEST['select1'] == '8'){
        redirect('http://example./somewhere.php');
    }elseif($_REQUEST['select1'] == '7'){
        redirect('http://example./elsewhere.php');
    }elseif($_REQUEST['select1'] == '6'){
        redirect('http://example./elsewhere.php');
    }
?>

<form>
<select name="select1" class="wpsc_select_variation" name="variation[5]" id="variation_select_48_5" onchange="this.form.submit()">
<option value="0" >-- Please Select --</option>
<option value="8" >Large</option>
<option value="7" >Medium</option>
<option value="6" >Small</option>
</select>

the only javascript required is within the select tag to trigger the submit onchange="this.form.submit()"

Hope this helps

$("select#wpsc_select_variation").change(function(){
    if ($(this).val() !== 0) {
        switch($(this).val())
        { 
            case(8):
                window.location.href = http://www.8.
            case(7):
                window.location.href = http://www.7.
            case(6):
                window.location.href = http://www.6.
        }
    }
});
发布评论

评论列表(0)

  1. 暂无评论