开发者

Keep select list choice after refresh

I have the following code that redirects the page (onchange) based on the value of option's choice. For example we are in index.php. There is a select list and if the user chooses开发者_高级运维 Design that has value "one" the redirected page will be the same page but with different url ( index.php&one )

My goal is to have the selected option, selected after the redirection/refresh.

Here is my code

<select size="1" name="Products"
onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php&'+this.options [this.options.selectedIndex].value">
<option value="">Please Select a Product</option>
<option value="one">Design
Software</option>
<option value="two">Manufacturing
Software</option>
<option value="three">Machine Tools</option>
</select>

Thanks for your time


A solution with javascript and jQuery framework:

$(document).ready(function(){
var option = gup('option');
$("select option[value='"+option+"']").attr('selected','selected');
});

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

Keep in mind that you have to change the & to ? and set a parameter and a value. like option=one.

So the corresponding javascript must change to this:

onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?=option'+this.options [this.options.selectedIndex].value">

Demo: http://lb.vg/969f6


Store data in cookies on change and reload when page (or other page) is loaded


You'll need to pass the variable along with the redirect. Then, get it out of the $_GET variable, and check to see if the value is set when building the select.

<?php
$menu_option = $_GET['product'];
?>
<select size="1" name="Products"
onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?product='+this.options [this.options.selectedIndex].value">
<option value="">Please Select a Product</option>
<option value="one"<?php if ('one' == $menu_option) { echo ' selected="selected"; } ?>>Design
Software</option>
<option value="two"<?php if ('two' == $menu_option) { echo ' selected="selected"; } ?>>Manufacturing
Software</option>
<option value="three"<?php if ('three' == $menu_option) { echo ' selected="selected"; } ?>>Machine Tools</option>
</select>


https://github.com/paulschreiber/misc/blob/master/php/popup_menu.php

I have a function that handles this. The version on github's a bit old, but should still work.

$productList = array (
"one" => "Design Software"
"two" => "Manufacturing Software"
"three" => "Machine Tools"
);

$js = "onchange=\"if(this.options.selectedIndex>0) window.location.href = 'index.php?Products='+this.options[this.options.selectedIndex].value\"";

print popup_menu("Products", $productList, $js);


<?php
$selected = 0; 
if(isset($_GET['Products']))
   $selected = $_GET['Products'];

?>

<select size="1" name="Products"
onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?Products='+this.options[this.options.selectedIndex].value">
<option value="">Please Select a Product</option>
<option value="one" <?php echo ('one' == $selected) ? 'selected="selected"' : '';?> >Design
Software</option>
<option value="two" <?php echo ('two' == $selected) ? 'selected="selected"' : '';?>>Manufacturing
Software</option>
<option value="three" <?php echo ('three' == $selected) ? 'selected="selected"' : '';?>>Machine Tools</option>


replace onchange with this one: onchange="if(this.options.selectedIndex>0) window.location.href = 'index.php?pro='+this.options [this.options.selectedIndex].value">

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜