0

A have a little problem with an url parameter.

I have a a form which uses method GET as I am only retrieving some data to get a resultlist. The data are not in any way secret to anyone. In short I can in the form input some measurements on a roof and those numbers will be used to calculated how many rooftiles etc. I have to buy. In the form I can also input a percentage of extra rooftiles to be calculated, f.x. 3% extra. After ending the input and pressing calculated I will get an url that look like this:

index.php?page=matliste&width=8&length=12&angle-1=45&extra=3&zip=9000&tagfod=tagfod&finish_stone=dv&topsten=utop&angle=45&mainhouse=1&house=1&rooftype=uu&lang=dk&rooftiletype=1&rid=1&producent=h

In this I have 3% extra. But when I go back to the form and input f.x. 5% it will not be updated. It just inserts a new parameter so it will look something like this:

extra=5&extra=3

How can I remove the existing parameter and insert a new one with the new number?

I am using this to build the query from the form fields:

if($_GET["newpage"] == "matliste") {   
    $get_elements = array_filter($_GET, "strlen");        

    $url = http_build_query($get_elements);

    $search_pattern = array();
    $search_pattern[0] = "/([?&]newpage)=[^&]*/";
    $search_pattern[1] = "/([?&]next)=[^&]*/";
    $search_pattern[2] = "/angle_\d+/";

    $search_replacements = array();
    $search_replacements[0] = '';
    $search_replacements[1] = '';
    $search_replacements[2] = 'angle';

    $new_url = preg_replace($search_pattern, $search_replacements, $url);

    header("Location: index.php?page=". $_GET["newpage"] ."&".$new_url);
}
8
  • duplicate of stackoverflow.com/questions/4100538/… Commented Aug 6, 2012 at 6:59
  • I am not sure how I can use the addUrlParameter with my example Commented Aug 10, 2012 at 11:23
  • replace your code, with that one. pass the URL to be modified, the URL variable name and its new value. Commented Aug 10, 2012 at 12:20
  • Can I do that dynamically? I don't know what parameter(s) to change as it is based on what the user which to change in the form. He might choose to enter only one new number in any given field. Commented Aug 10, 2012 at 12:41
  • You might think I am completely stupid, but can you give me an example? :) I have looked at this so much that I am totally blind of finding the solution. Commented Aug 14, 2012 at 7:13

1 Answer 1

0

Instead of doing all that, just stick the values you have back into the form and let the browser recreate the query string for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.