simple but not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cobby64
Forum Newbie
Posts: 2
Joined: Wed Feb 25, 2009 4:55 am

simple but not working

Post by cobby64 »

Hi I'm a newbie and i have a problem this thing i'm doing. Here's the code

Code: Select all

<body>
 
<?php if (!isset($_GET['name'])): ?>
 
   <!-- No name has been provided so we prompt the user -->
   
   <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
   <label>Please enter your name:<input type="text" name="name" /></label>
   <input type="submit" value="GO"/>
   </form>
 
<?php else: ?>
 
 
   <p>Your name: <?php echo $_GET['name']; ?></p>
   
   <p>This paragraph contains a <a href="welcome.php?name=
      <?php echo urlencode($_GET['name']);?>">link</a> that passes the name
      variable on to the next document.
   </p>
      
<?php endif; ?>
 
</body>
This is what I want to achieve:
1. The page loads with the Please enter your name bit
2. If no name is entered, the page should be reloaded and the user presented with the same thing. (echo $_SERVER['PHP_SELF'])
3. When a name is provided, then the else statement should load, that is "Your Name: {URL.Name} and then what follows.

The problem i have is that whether or not i enter a name, I still get the "Your Name:..." part under the else statement. Is my control statement wrong?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: simple but not working

Post by requinix »

cobby64 wrote:The problem i have is that whether or not i enter a name, I still get the "Your Name:..." part under the else statement. Is my control statement wrong?
Not really, it's doing what it is supposed to do.

When you leave the box empty you are using an empty string for $_GET["name"]. The thing still exists (which is what isset checks for) - there's just nothing in it.

For a simple check that there's something in there, try using empty instead of !isset.
cobby64
Forum Newbie
Posts: 2
Joined: Wed Feb 25, 2009 4:55 am

Re: simple but not working

Post by cobby64 »

tasairis wrote:When you leave the box empty you are using an empty string for $_GET["name"]. The thing still exists (which is what isset checks for) - there's just nothing in it.
Thanks... The empty() function works just the way i want it to. I guess the isset() didn't work because an empty string was considered as a value and so the output was always false.
Post Reply