0

I thought this would be an easy endeavor, but it turned out complicated.

So I've an url with following gets:

?mt-make=ford&mt-model=fiesta

Form looks like this:

enter image description here

So when user clicks Trash icon, I want to unset _GET variable.

<?php foreach ( $_GET as $k => $v ) { ?>
<?php if ( $k == 'mt-vehicle' || empty( $v ) ) { continue; } ?>
<li class="mt-search-tags-list-item"><a href="#"><?php echo $v; ?> <span class="dashicons dashicons-trash"></span></a></li>
<?php } ?> 

How would go about doing this?

I tried instead of link having a button with name="mt-make" with empty value thinking it would overwrite _GET in the url - but it didn't! that was unexpected. Instead I got this ?mt-make=&mt-make=ford&mt-model=fiesta notice double mt-make!

  • 1
    PHP runs on the server-side, meaning that the page is generated on the server-side before sending it to the user. The `$_GET` variables are already "set in stone" when the page is visible on the browser, and unsetting it will not change the page as you expect it to be (in a "live" manner). Is there a reason why you want to modify the URL? We might be looking at an X-Y problem. In fact, you should be [using JS to update the query string](http://stackoverflow.com/questions/1090948/change-url-parameters), and push to history if needed. – Terry Nov 02 '16 at 21:50
  • @Terry notice that I don't want it to change it in LIVE manner, I understand that much. please check `I tried` section. –  Nov 02 '16 at 21:54
  • You still have to use JS. When you click on the trash can, you set the values of the `mt-make` and `mt-model` to empty, and then submit the form manually. In that case you will get query strings with no values, i.e. `&mt-make=&mt-model=`. – Terry Nov 02 '16 at 21:56
  • @Terry we are not understanding each other. This is what I am expecting, but I'm not getting it. –  Nov 02 '16 at 21:57
  • I am not sure if you understood my previous comment. Use JS to reset the appropriate fields when clicking on the trash can, and then submit the form programatically. That should empty the fields. You have not used any JS in your attempt, that's why you're not getting what you expect. – Terry Nov 02 '16 at 21:59
  • @Terry got it now. Where can I read about name attributes behaviour? and why its not overwriting same name _GETs? –  Nov 02 '16 at 22:00
  • `name` should be unique for a form. Setting it multiple times will not override it—the browser will treat it separately. Some server-side scripts will parse multiply-defined `name` attributes as an array. – Terry Nov 02 '16 at 22:02
  • @Terry well php parses array if its `name[]`. anyhow, thanks for explanation. –  Nov 02 '16 at 22:08
  • There is no standard on multiply-defined keys in query string parameter: see http://stackoverflow.com/questions/24059773/correct-way-to-pass-multiple-values-for-same-parameter-name-in-get-request – Terry Nov 02 '16 at 22:11

1 Answers1

0

You need to generate the "trash" link server side. I suggest you incapsulate link generation into a function. Here's something for you to start. Pass the name of the parameter you would like to exclude:

function generateTrashUrl($propToExclude)
{
    // Let's create an array from GET query
    $queryParams = [];
    parse_str($_SERVER['QUERY_STRING'], $queryParams);

    if ($queryParams) {

        // Now let's check if required parameter is in the array; if it is - get it out
        if (array_key_exists($propToExclude, $queryParams)) {
            unset($queryParams[$propToExclude]);
        }

        // Generate base of the resulting URL
        $result = 'http://' . $_SERVER['HTTP_HOST'];

        // Transform our modified parameters array into query string and append it to URL base
        return $result .= http_build_query($queryParams);

    }
}
Georgy Ivanov
  • 1,573
  • 1
  • 17
  • 24