INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

"isset" not working :-(

"isset" not working :-(

(OP)
Hi everyone,
A very simple code that aims to know whether value was inserted to an input field in a form is not working sad.
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 ! 

RE: "isset" not working :-(

Hi

You should involve isset() 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 http://localhost/myIsset.php?a=&b=, var_export($_GET) will show :

CODE --> var_export($_GET)

array (
  'a' => '',
  'b' => '',
) 
So $_GET['a'] being set, isset($_GET['a']) will evaluate to true.

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

Feherke.
feherke.github.io

RE: "isset" not working :-(

(OP)
Thanks a lot feherke.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close