最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Redirect with HTML dropdown select - Stack Overflow

matteradmin7PV0评论

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.
        }
    }
});
Post a comment

comment list (0)

  1. No comments so far