Solved

<select> redirect when a item is selected

Posted on 2006-10-28
8
212 Views
Last Modified: 2008-03-06
I have a <select> with some items in it. I want to reload the page with the parameter selected in the <select> box, when a item is selected.

Someone gave me this script

     <script type="text/javascript" language="Javascript">
          function reloadPage(pSelect)
          {
               if (!pSelect || pSelect.value.length == 0) return;
               
               window.location = location.pathname + '?' + pSelect.name + '=' + pSelect.value;
          }
     </script>

But there is one issue left, my page already got parameters.
http://localhost/progress/index.php?p=admin&ap=manage_crafts

This function makes it to:
http://localhost/progress/index.php?recipe_type=Alchemy
instead of:
http://localhost/progress/index.php?p=admin&ap=manage_crafts&recipe_type=Alchemy

How I can fix this ?

Also note that in the reloadPage function I I tried to change:
window.location = location.pathname + '?' + pSelect.name + '=' + pSelect.value;
with:
window.location = location.pathname + location.search +'&' + pSelect.name + '=' + pSelect.value;

After one selection my link is as I want it, but after second selection it gets:
http://localhost/progress/index.php?p=admin&ap=manage_crafts&recipe_type=Alchemy&recipe_type=Engineiring
And so on.

It adds a new &recipe_type=... instead of replacing the old one.

How I can fix this please ?
0
Comment
Question by:brightwood
  • 4
  • 4
8 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 17826796
Try:

var newLocation = location.href;
if(newLocation.indexOf("?") > -1) {
  newLocation += "&" + pSelect.name + "=" + pSelect.value;
} else {
  newLocation += "?" + pSelect.name + "=" + pSelect.value;
}
window.location = newLocation;

-r-
0
 

Author Comment

by:brightwood
ID: 17826823
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17826948
When pSelect's name always is recipy_type, then you can use:

var newLocation = location.href.replace(/recipy_type=\w+/g,'');

Alternatives would be to construct a RegExp object.

-r-

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:brightwood
ID: 17826990
                       var newLocation = location.href;
                        if(newLocation.indexOf("recipe_type") > -1) {
                          newLocation = location.href.replace(/recipe_type=\w+/g,'');
                        } else {
                          newLocation += "&";
                        }
                        newLocation += pSelect.name + "=" + pSelect.value;

                        window.location = newLocation;

does what I need.

But is there a way to use pSelect instead of "recipe_type"
in newLocation.indexOf("recipe_type")
and
location.href.replace(/recipe_type=\w+/g,'');
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 17826995
Try:

var reg = new RegExp(pSelect.name + "=\\w+", "gi");
var newLocation = location.href.replace(reg,'');

                    if(newLocation.indexOf("?") > -1) {
                      newLocation += "&";
                    } else {
                      newLocation += "?";
                    }
                    newLocation += pSelect.name + "=" + pSelect.value;

-r-
0
 

Author Comment

by:brightwood
ID: 17827014
Worked, with a little modification.
var reg = new RegExp("&" + pSelect.name + "=\\w+", "gi");
instead of
var reg = new RegExp(pSelect.name + "=\\w+", "gi");

& would get duplicated every select without this little change.

Thanks a lot, you were a great help.
0
 

Author Comment

by:brightwood
ID: 17827024
Wops just a little question.

var reg = new RegExp("&" + pSelect.name + "=\\w+", "gi");
var newLocation = location.href.replace(reg,'');

This code replace &pSelect and everything after it with '' So if I have another variable after this it gets lost.

How can I do it to replace everthing &pSelect and everthing until another & with '' ?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 17828279
Are you sure? If i run this script below, it works fine:

<script type="text/javascript">
pSelect = {name:"b"}
var reg = new RegExp("&" + pSelect.name + "=\\w+", "gi");
var uri = "test.php?a=1&b=2&c=3";
var newLocation = uri.replace(reg,'');
document.write(newLocation);
</script>
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question