i am trying to append more than 2 variables in URL using location.href in javascript. Here is the code:

location.href='Add_New_Project.php?project_name=' + project_name + '&cat=' + val + '&prj_desc=' + project_desc;

The page is suppose to refresh when the code executes, but it wouldn't. Whereas if i try it with only two variables minus these ('&prj_desc=' + project_desc), it actually reloads and give me the right answer:

location.href='Add_New_Project.php?project_name=' + project_name + '&cat=' + val;

What are my not doing right? Please i need urgent help.Thanks

5 Years
Discussion Span
Last Post by fobos

try using
window.location.href = 'Add_New_Project.php?project_name=' + project_name + '&cat=' + val + '&prj_desc=' + project_desc;


I generally build query strings like this:

queryString = [];
queryString.push('project_name=' + project_name);
queryString.push('cat=' + val);
queryString.push('prj_desc=' + project_desc);
location.href = 'Add_New_Project.php?' + queryString.join("&");

Thus the line number in any js error message will be more specific than when the url is built in one line.


Votes + Comments
Nice, clean and simple. I like it.

@ Airshow and fobos, thanks a lot, but it still didn't work. In anycase, how would you guys have solved this problem your own way:

you have a form with 1 textbox(to collect username),another textbox(to collect address), 1 textarea(to collect description), 1 select box to collect categories, another select box to collect sub-categories.

now getting sub-category depends on the category value selected and both is being populated from a database(now if your page must refresh for this to happen), then whatever user may have typed into the username,address and description boxes before page refreshes should not disappear from those boxes.

Please endeavor to test your script to be sure that it's working so that if it doesn't work over here, then i'll be sure that the fault is from me or my host.

Thank you so very much in advance.


Two points:

  1. Like the code in your original post, my code (and fobos' code) is only a snippet. It won't work on its own. It relies on the variables project_name , val , project_desc having been established, each reflecting the user-entered values of their respective fields.
  2. Refreshing the page will destroy the fields' values unless the page is re-served with the fields re-populated with those values. To obtain a response from a server-side script without refreshing the whole page, you need to use AJAX techniques.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.