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);
}