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

Recommended Answers

All 7 Replies

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

Member Avatar

Is there a JavaScript error?
What is the value of 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.


commented: Nice, clean and simple. I like it. +7

@ 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.


Are you still havig problems?? If not, MARK THE THREAD SOLVED!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.