2

How do i stop the submit being shown in the search bar? am i doing something wrong? if i use post method it works fine, but recently i've tried using the get method and it prints both submit and the textbox text in the bar, i tried removing the name="submit" and it didn't do anything when i clicked it. SO how do i stop submit being shown in the address bar?

at the moment it give me $submit=submit, :(

ty all.

<form action="" method="get">
<input type="text" name="website" placeholder="Website Name">
<input type="submit" name="submit">
</form>
<?php
if(isset($_GET['submit']))
{
  if(!empty($_GET['website']))
  {
    //do stuffs here
  }
  else
  {
     //else echo out that nothing was entered.
   echo "nothing entered";
  }
}
?>
Ankur Bhadania
  • 4,123
  • 1
  • 23
  • 38

2 Answers2

1

Just use:

!empty($_GET)

In your first if statement

So your code should look something like this:

<form action="" method="get">
    <input type="text" name="website" placeholder="Website Name">
    <input type="submit">
</form>

<?php

    if(!empty($_GET)) {

        if(!empty($_GET['website'])) {
            //do stuffs here
        } else {
            //else echo out that nothing was entered.
            echo "nothing entered";
        }
    }

?>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

To remove the variables from the URL bar, you need to use the POST method instead of the GET method.

So, this form code should work for you:

<form action="" method="POST">
    <input type="text" name="website" placeholder="Website Name">
    <input type="submit" name="submit">
</form>

You'll also need to alter your PHP code to use $_POST instead of $_GET.

The downside to this is that it won't show the submitted website in the URL bar, meaning users can't bookmark a submitted form. However, this may also be intended.

Alternative Solution

You can remove the name from the submit, and use isset($_GET) to test whether the form was submitted instead.

Here is an example of how this could be done:

<form action="" method="get">
    <input type="text" name="website" placeholder="Website Name">
    <input type="submit">
</form>
<?php
if(isset($_GET) && count($_GET) > 0) {
    if(!empty($_GET['website']))
    {
        //do stuffs here
    } else {
        //else echo out that nothing was entered.
        echo "nothing entered";
    }
}
?>
Nerixel
  • 427
  • 3
  • 9
  • But i want only the $website=blahblah to be in the address bar, not the submit button... i've used post a gazillion times so i know how it works, but the get method i do not :/ – Клаире Березовский Nov 22 '14 at 11:03
  • 1
    @Nerixel don't use the `isset` check as $_GET is always set. Better to use `empty` – DarkBee Nov 22 '14 at 11:10
  • `empty` will error if the variable's not set. `isset` is a less error-prone function. Ideally you'd combine them as in `isset($_GET) && !empty($_GET)`, so I might as well edit that in. – Nerixel Nov 22 '14 at 11:11
  • When i load the page, it automatically says under the textbox "nothing entered" and i havn't even touched it..? why is that? – Клаире Березовский Nov 22 '14 at 11:12
  • @Nerixel This is completely unnecessary and WRONG to combine this: `isset($_GET) && !empty($_GET)` See: http://stackoverflow.com/q/4559925/3933332 empty() replace isset with the addition to check that it's not empty! – Rizier123 Nov 22 '14 at 11:32
  • Jeez, relax, no need to textually shout at me. All I know is that I used to get errors when not including `isset()`. Don't know why, and I don't get them anymore, I just include it out of habit. – Nerixel Nov 22 '14 at 11:39
  • @Rizier123 Besides, I realised midway through that it'd be better to count the array rows and check that rather than using `empty()`, so that's not even relevant to the answer. I just didn't bother editing my comment. – Nerixel Nov 22 '14 at 11:42