Remove a parameter from URL after form submit (GET)

Hi,

I have the following form:

<form action="/form.php" method="get">
	<input type="text" name="term">
	<input type="submit" name="search" value="Search">
</form>

When I submit the form, the form.php page has the following URL:

/form.php?term=myterm&search=Search

I want only the term parameter to appear in the URL, &search=Search is to be removed, is that possible? Or any alternative way by using POST?

Thanks.

If you used POST it wouldn’t appear in the URL.

So, how can I use POST and have term parameter appear in the URL?

form.php?term=myterm

Sorry, I only half-read that. I’m not sure it’s possible, this blog posting seems similar:

Out of interest, can I ask why you want the search term to stay in the URL?

Thanks for the link.

I want the same behavior as YouTube does:

https://www.youtube.com/results?search_query=test+search

So, user can also see their search term in the address bar.

Seems the only way is to remove the name attribute from the submit button. I will go with that one.

you can omit the name on the submit button, but then any check for the submit button obviously will fail.

1 Like

Perhaps a hidden input to get around this?

Thanks, that’s what I will go with for the moment.

You could always make a page called setpage.php and use this to reformulate the url and send you back to form.php so you are not posting to the same page.

<?php
//Define any acceptiable GETs

    $mode = array();
    if(isset($_GET['term'])):
        $mode[] = 'term='.$_GET['term']; 
    endif; 
    
    if(isset($_GET['id'])):
        $mode[] = 'id='.$_GET['id'];
    endif; 
    
    if(isset($_GET['error'])):    
        $mode[] = 'error='.$_GET['error'];
    endif;
    
    //etc...
    
    //Then reformulate link 
    if(!empty($mode)){
        $link = '?' . implode('&',$mode);
    }else{
        $link = '';
    }
    header("location: form.php".$link);
exit();
?>

I realize this is in the PHP category, but why not use javascript to grab the search term and redirect the user:

var form = document.getElementById("myForm");
form.addEventListener("submit", function(e){
    e.preventDefault();
    var term = document.getElementById("text").val;
    window.location.href = "form.php?term=" + term;
});

ALL inputs will be shown in the url if you use GET , NONE if you use post. The TRUE reason to use use GET is to make the URL bookmarkable.

As such you probably want to show BOTH the data ( term) and the action (search)… unless for some reason your script automatically searches? so then you don’t need the name on the search button and you can stick with GET. the form will submit, just no value will be passed for that button. You can ten check for one of the other required fields on the form as a test for submittal, perhaps?

1 Like

Thanks all for the suggestions. I learned from your replies. Though, I don’t want to add any additional steps or extra code (hidden input, redirects, javascript etc.) whenever a simpler alternative is possible. Simplicity is what I seek while coding so I removed the name attribute from the submit button as Dormilich and dresden_phoenix suggested and that works just fine for me.

Thanks again.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.