0

I am trying to use the addUrlParameter function from this php - add/update a parameter in a url

but I just can't get it to work for my specific problem and I hope in desperation that someone can help me.

As of now I am using this to handle a form and create a URL from the form post

$get_elements = array_filter($_GET, "strlen");        
$url = http_build_query($get_elements);

$search_pattern = array();
$search_pattern[0] = "/([?&]newpage)=[^&]*/";
$search_pattern[1] = "/([?&]next)=[^&]*/";

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

$new_url = preg_replace($search_pattern, $search_replacements, $url);
header("Location: index.php?page=". $_GET["newpage"] ."&".$new_url);

This is fine, but when I go a step back and input a new value for a certain parameter, that new value is not updated in the new URL - it uses the old value.

Lets say I fill in that I want 4 items of a certain product and post that form that goes to the next page. Then I realise that I wnat 7 items of that product - then I go a step back and input 7 and post again. My problem is that it doesn't update the parameter of that product with 7, it just insert a new parameter of the same name with 7 as value, but the final url uses the parameter with 4 as the value

i.e.:

http://www.domain.com/formhandler.php?vent_45=&udluftning_plast=7&udluftning_fald=&udluftning_ventitegl=&tagfod=fuglegitter&chimney_afstand_tagrende=&udluftning_plast=4&tagfod=fuglegitter&afslutning_sten=dv&topsten=utop&angle_1=45&mainhouse=1&house=1&rooftype=mu&lang=dk&rooftiletype=1&rid=1&producent=h&newpage=inputdims&next=N%C3%A6ste+trin+%C2%BB

Where the parameter in this example is udluftning_plast - the URL have two of that parameter, but uses the last one - I need it to remove the old one and replace it hwith the new value.

Then I tried to use the addUrlParameter function from the link above, but I just can't get it to work.

The tricky part here is that on the page to fill in quantities of items there a different items to update so it shall update the url dynamically somehow.

EDIT:

I can see that the addUrlParameter function removes every dublicate parameter at the beginning of the query string where the new values are set. So that could be the problem. What am I doing wrong?

If I var_dump($url_data) at the beginning of addUrlParameter I get this URL with all the parameters:

array(4) { ["scheme"]=> string(4) "http" ["host"]=> string(22) "www.domain.com" ["path"]=> string(19) "/ny/formhandler.php" ["query"]=> string(431) "vent_45=&udluftning_plast=6&udluftning_fald=&udluftning_ventitegl=2&tagfod=fuglegitter&chimney_afstand_tagrende=&vent_45=&udluftning_plast=4&udluftning_fald=&udluftning_ventitegl=2&tagfod=fuglegitter&chimney_afstand_tagrende=&afslutning_sten=dv&topsten=utop&angle_1=45&mainhouse=1&house=1&rooftype=mu&lang=dk&rooftiletype=1&rid=1&producent=h&newpage=venting&next=N%C3%A6ste+trin+%C2%BB&newpage=inputdims&next=N%C3%A6ste+trin+%C2%BB" }

when I var_dump($url_data) at the end of the function I get this URL with the updated parameters removed:

array(4) { ["scheme"]=> string(4) "http" ["host"]=> string(22) "www.domain.com" ["path"]=> string(19) "/ny/formhandler.php" ["query"]=> string(274) "vent_45=&udluftning_plast=4&udluftning_fald=&udluftning_ventitegl=2&tagfod=fuglegitter&chimney_afstand_tagrende=&afslutning_sten=dv&topsten=utop&angle_1=45&mainhouse=1&house=1&rooftype=mu&lang=dk&rooftiletype=1&rid=1&producent=h&newpage=inputdims&next=N%C3%A6ste+trin+%C2%BB" }

I am calling the function this way:

foreach($_GET as $key => $value) {      
    addURLParameter("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", $key, $value);
}
4
  • if I do this: addURLParameter("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", "udluftning_plast", 7); it works, but this is obviously very wrong as I have no idea with parameters will be updated and with which values. Commented Feb 21, 2013 at 9:21
  • actually, that is exactly what you want to do - the function either adds a new parameter or updates an existing one. Commented Feb 21, 2013 at 12:15
  • Oh ... Can you guide me in the right direction on how to update an existing parameter? Commented Feb 21, 2013 at 13:20
  • See my updated answer for a form submission example. Commented Feb 22, 2013 at 11:08

1 Answer 1

0

addURLParameter is a function that returns a new url with either a new parameter inserted into the url provided, or (if the parameter is already present in the url provided) an existing parameter updated with a new value.

So, if you had:

$myUrl = http://www.mysite.com/mypage.php?a=1&b=2

and you executed

$myNewUrl = addURLParameter($myUrl, "c", 99);

$myNewUrl would be

http://www.mysite.com/mypage.php?a=1&b=2&c=99

if you were to execute

$myNewUrl = addURLParameter($myUrl, "b", 99);

$myNewUrl would be

http://www.mysite.com/mypage.php?a=1&b=99

So, if you needed to update just "udluftning_plast" based on a value passed in from a form submission:

$udluftning_plast_val = $_GET["udluftning_plast"];
$url = addURLParameter("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]", "udluftning_plast", $udluftning_plast_val);

If the form was submitted with the "udluftning_plast" field having a value of 99, then $url will contain a single reference to udluftning_plast: ...&udluftning_plast=99...

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

2 Comments

If I set that parameter manually, it works perfect, but my problem is that the value is set by a user input and he/she can fill in any number. And on the page where the field for "udluftning_plast" there are other fields as well that can be filled in. So I need it to remove the old parameter and replace it with the new one - or update the parameter if you will.
Hmm.. That didn't seem to work! I must to something wrong at another place.

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.