Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"isset" not working :-( 1

Status
Not open for further replies.

lupidol

Programmer
Apr 23, 2008
125
IL
Hi everyone,
A very simple code that aims to know whether value was inserted to an input field in a form is not working :-(.
In the following code, when a value is inserted to field 'A" I want 'a' to be echoed and if 'b' recieves value the program should
show 'b' on the display.
Whether I insert a value or weather no value is inserted at all the following program always shows shows both 'a' and 'b'.
Can anyone help me with mnaking that program show 'a' only when 'a' was inserted value and 'b' only when be was inserted value?
Code:
<?php //myIsset.php
$a = '';
$b = '';
if (isset($_GET['a'])) echo 'a<br>';
if (isset($_GET['b'])) echo 'b';

echo '
<!DOCTYPE html>
<html lang = "en">
<head>
  <title>myIntval.php</title>
  <meta charset="utf-8" />
</head>

<body>
  <form action = "myIsset.php" method = "GET">
    A <input type ="text" name = "a">
    B <input type ="text" name = "b">
    <input type = "submit" value = "convert">
  </form>
</body>
</html>';
?>
Thanks !
 
Hi

You should involve [tt]isset()[/tt] only in logic to see whether a and b were actually sent to avoid you code crashing in their absence.

Given the empty form will be submitted as [ignore][/ignore], [tt]var_export[teal]([/teal][navy]$_GET[/navy][teal])[/teal][/tt] will show :
Code:
[b]array[/b] [teal]([/teal]
  [i][green]'a'[/green][/i] [teal]=>[/teal] [i][green]''[/green][/i][teal],[/teal]
  [i][green]'b'[/green][/i] [teal]=>[/teal] [i][green]''[/green][/i][teal],[/teal]
[teal])[/teal]
So [tt][navy]$_GET[/navy][teal][[/teal][green]'a'[/green][teal]][/teal][/tt] being set, [tt]isset[teal]([/teal][navy]$_GET[/navy][teal][[/teal][green]'a'[/green][teal]])[/teal][/tt] will evaluate to [tt]true[/tt].

The simplest in your case is to use [tt][teal]![/teal] empty[teal]([/teal][navy]$_GET[/navy][teal][[/teal][green]'a'[/green][teal]])[/teal][/tt] instead.
Though things used to be more complex in practice : an [tt]isset[teal]([/teal][navy]$_GET[/navy][teal][[/teal][green]'a'[/green][teal]])[/teal][/tt] based [tt]if[/tt] controls whether a block with more detailed validations gets executed in case the parameter was received.


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top