-1

I have a page on which users fill out a form to register their team:

<html>
<form name="newteam" method="get" action="regnewteam.php?team_name=$team_name&member1=$member1">
  <tr>
    <td>
      Register Team Name:
    </td>
    <td>
      <input name="team_name" type="text" value="<?php echo $team_name;?>">
      <?php settype($team_name, 'string');?>
    </td>
  </tr>
  <tr>
    <td>
      Member N1:
    </td>
    <td>
      <input name="member1" type="text" value="<?php echo $member1;?>">
      <?php settype($member1, 'string');?>
    </td>
  </tr>
  <tr>
    <td></td>
    <td>
      <input type="submit" id="submit" value="Register">
    </td>
  </tr>
</form>

</html>

Once they submit it I check if the two fields are full in regnewteam.php using if (isset($_GET['team_name']) && isset($_GET['member1'])), but when the fields are empty it still executes the code in the if statement. Why is it doing this?

Oliver
  • 821
  • 5
  • 12
  • 28
  • 1
    It will do, empty text boxes are still passed through in the HTTP header (except `textarea`, `radio` and `checkbox` fields) ... you should use `!empty()` instead of `isset()` – CD001 Aug 27 '15 at 14:06
  • *"And once they submit it I check if the two fields are full using if (isset($_GET['team_name']) && isset($_GET['member1'])),"* and where is that implemented in your code? It's not shown. Plus, what is your posted code's filename, regnewteam.php? – Funk Forty Niner Aug 27 '15 at 14:13
  • 2
    Just a point: The querystring here on your form tag `action="regnewteam.php?team_name=$team_name&member1=$member1"` will be doing nothing as when the form initially loads you presumably dont actually know what is going to be entered. Change it to `action="regnewteam.php"` to avoid any confusion – RiggsFolly Aug 27 '15 at 14:22
  • *We're all bubble blowers Smokey* - @RiggsFolly Can we all take a magic carpet ride? – Funk Forty Niner Aug 27 '15 at 14:35
  • Thank you for your participation. – Funk Forty Niner Aug 27 '15 at 14:42
  • Alakazam. And with a puff of _smokey_ There you go Ralph, borrow my carpet anytime @Fred-ii- – RiggsFolly Aug 27 '15 at 14:48
  • 1
    I don't understand how this is a duplicate of the other question. He never mentioned notices of undefined variables or indexes. He's saying that empty variables are passing `isset` (which they will if set). – Michael Aug 27 '15 at 14:52

3 Answers3

6

Use empty() instead of isset() - empty will check if variable is empty and set at the same time.

Tomasz
  • 4,847
  • 2
  • 32
  • 41
  • 1
    `!empty()` rather than `empty()` but otherwise yeah ;) – CD001 Aug 27 '15 at 14:07
  • 1
    Yes you are right in this particular example from @Oliver we should use `!empty()` – Tomasz Aug 27 '15 at 14:12
  • Thing is, this doesn't solve the immediate issue, of the undefined variables in their form's input values; they'll still get undefined variables notices. OP stated *"Once they submit it I check if the two fields are full using if (isset($_GET['team_name']) && isset($_GET['member1']))"* - which isn't shown in their question and I for one, deem the question as unclear. Personally, I would do it another way. – Funk Forty Niner Aug 27 '15 at 14:27
  • @Fred-ii- you entire contribution to the answer on this page, has just been criticism, which isn't very helpful either ;) – Oliver Aug 27 '15 at 14:42
  • 1
    @Oliver that's the thanks I get? *censored* - Your question's a duplicate and doesn't make any sense as RIggs pointed out too. – Funk Forty Niner Aug 27 '15 at 14:42
  • @Fred-ii- sorry man, I got my answer so I am happy – Oliver Aug 27 '15 at 14:44
  • @Oliver yeah, figured as much. *classic* – Funk Forty Niner Aug 27 '15 at 14:44
2

isset() just checks if is it set, it could be anything not null,where as empty() checks if the variable is set and if it is it checks it for null, "", 0, etc. Changing

isset($_GET['variable']) 

to

if(!empty($_GET['variable'])) 

will be a better test in the way you're trying to check this.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
1

Although the other answers suggest using !empty, this will return false if the variable is the string (or integer) 0 which might not be what you want. An alternative is:

isset($_GET['variable']) && '' !== $_GET['variable']
Michael
  • 11,912
  • 6
  • 49
  • 64
  • `(int) 0` qualifies as empty (as it's falsey)... but yeah, I see your point with `(string) "0"` and since it's a GET var... – CD001 Aug 27 '15 at 14:12