views:

62

answers:

5

Hey Guys, I have the following 3 urls:

http://www.test.com?a=1
http://www.test.com?a=1&b=3
http://www.test.com?a=1&b=2&c=99

Now in a form i have a drop down menu like so:

<select name="b">
    <option value="1">1</option>
     ...
</select>

Now i want to either add that param to the list of existing params or edit the param if its already there, then refresh the page. Any ideas? Thanks

A: 

If i submit the form then it doesnt add the GET params that are already there to the url

doubtful
You can comment on and edit your own questions, it's best not to leave answers that should be comments or edits.
Andy E
i cant add a comment to the comments under my post
doubtful
Donal Boyle. Im using php
doubtful
you should edit you question.
arnorhs
+1  A: 

You can do it the regexp way:

var myNewValue = 1; // the new value of input named b
var url = window.location.href;
if(url.match(/\Wb=/)) url = url.replace(/(\Wb=)[^&]*/, "$1"+myNewValue);
else url = url+"&b="+myNewValue;
window.location.href = url;
Alsciende
A: 

You should write the parameters you want to keep as hidden inputs in the new form My PHP is rusty, but here goes:

<input type="hidden" name="a" value="<?php echo (int)$_GET['a']; ?>" />

JavaScript does not have direct access to the request parameters for the page, unless you use regex to parse the parameter list. However, the server side technology, in your case PHP, does have access when it is generating the page.

By creating hidden inputs in the form, the user will not have the option to change the values, but they will get included when the form is submitted again.

Donal Boyle
A: 

I'm not sure if you're using PHP or javascript for this problem, since you tagged the question with both.

With PHP your other parameters will disappear when you submit the form, unless you include the old parameters in the form as well, so for you example:

<select name="b">
    <option value="1">1</option>
    ...
</select>
<input type="hidden" name="a" value="<?php echo htmlentities(@$_GET['a']); ?>" />
<input type="hidden" name="c" value="<?php echo htmlentities(@$_GET['c']); ?>" />

However, if you're using Javascript, like the other guys have mentioned you will have to parse the query string, but essentially do the same: Add the other parameters to your location.

arnorhs
A: 

"Now i want to either add that param to the list of existing params or edit the param if its already there"

I'd go with parse_str() / parse_url() functions to split the URL into associative array, then you can add/update array element with your new value and combine it back to valid URL with http_build_url(). Please have a look at examples in php manual, it's easier to code it than to write about it ;)


"then refresh the page"

The answers about hidden form elements aren't bad but I wonder if user will understand what happens (he will have no visible confirmation that his previous choice is remembered, yet you'll somehow save it). Because of that I'd go a bit different path: assuming that forms are generated with php and aren't static HTML, why won't you just make sure that the options chosen in first form submit are highlighted when you show the form for subsequent submits?

<select name="b">
    <option value="1" <?php if(isset($params['b']) && $params['b'] == 1) echo 'selected="selected"'; ?>>1</option>
</select>

It looks a bit messy but if you'll make habit of re-displaying user's input you save them typing (they'll hate your page if they'll have to fill an emptied form 3 times because of some validation).

For option tags the proper atribute is "selected", for checkboxes/radio buttons it's "checked", text inputs have attribute "value" where you can enter whatever you want etc. Not sure if I'm not talking about something obvious to you...

eyescream