-1

I have a search form, and the submit button looks like this:

<input type="submit" name="search_submit" value="Go"/>

I handle this form using the following php:

if (isset($_GET['search_submit'])) {
    do blah
}

Works fine. But my url then includes &search_submit=Go. I do not want that to show up.

I know that to fix this, I need to remove the name attribute from my the forms input line.

But then my php no longer works and I'm not sure how to change it to handle the form. I tried changing it to:

if (isset($_GET['submit']))

and

if (isset($_GET['Go']))

But they did not work either. If anyone can help me with an answer, it would be awesome.

Wilson
  • 176
  • 1
  • 11
jotty
  • 95
  • 7
  • 1
    Why don't you POST the Form or use AJAX instead of using URL's to GET the Data – Tasos Jul 01 '14 at 08:37
  • @Darren An input field that's being submitted still shows up in the URL. – padarom Jul 01 '14 at 08:42
  • That's a good idea. It is possible to pass a hidden field along with the submit button? Woops nevermind, just read Padarom's reply :) – jotty Jul 01 '14 at 08:43
  • if you don't want (&search_submit=Go) why are you putting that in the URL in the firstplace. Can you add the code on how you compile the URL to have a look – Tasos Jul 01 '14 at 08:44

3 Answers3

1

You cannot remove the name of the input element, as PHP would not know which value to look for. If you want to completely hide the string after the URL, use the request method POST instead of GET:

<form action='myscript.php' method='POST'>
    <input type="submit" name="search_submit" value="Go"/>
</form>

Your PHP will use the following:

$_POST['search_submit']; // Instead of $_GET['search_submit'];

A good answer to when to use GET and POST can be found here.


edit: If you just want to not have the button show up in the URL, but everything else should still be there (according to your comment), you can simply remove both the value and name of the submit button.

Instead of looking for search_submit to be set, you can look for the other values:

if (isset($_GET['username'], $_GET['password'])) {
    // Do your stuff here
}
Community
  • 1
  • 1
padarom
  • 3,529
  • 5
  • 33
  • 57
  • That's what I was afraid of. Unfortunately I need to use GET, as every part of my form, except the submit button, needs to be in the url (for browser back button/bookmarking functionality). So I'm either stuck with submit showing up in the url too, or hopefully there's another trick? – jotty Jul 01 '14 at 08:37
  • Hm, thanks Padarom. I think this might be my only option, though I will have to come up with a trick to use it...the initial GETs (everything in the rest of the form) are set by default as the form is loaded..so I can't check for them, as they are set already before I submit. The only thing that isn't set by default, of course, is the submit button :) But I think I can use the idea behind your solution to come up with something... thanks for the tip :) – jotty Jul 01 '14 at 08:49
1

If you don't want to show string in the URL, you can use the POST method. The main difference between GET and POST are listed below as :

GET:

  • Parameters remain in browser history because they are part of the URL Can be bookmarked.
  • GET method should not be used when sending
  • passwords or other sensitive information.
  • 7607 character maximum size.
  • Url example: new.php?category=sport

POST:

  • Parameters are not saved in browser history.
  • Can not be bookmarked.
  • POST method used when sending passwords or other sensitive information.
  • 8 Mb max size for the POST method.
  • URL example: new.php

Sample Code :

if (isset($_POST["search_submit"])) {
    do blah
}
nawazlj
  • 779
  • 8
  • 18
0

If the submit button doesn't have a name, then it won't be a successful control and won't appear in the submitted data at all.

Test for the presence of data from some other field in the form instead.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Wish I could upvote this at least, and some of the other posts. not enough rep yet though :( – jotty Jul 01 '14 at 09:49