Page 1 of 1

simple but not working

Posted: Wed Feb 25, 2009 11:14 am
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?

Re: simple but not working

Posted: Wed Feb 25, 2009 5:00 pm
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.

Re: simple but not working

Posted: Thu Feb 26, 2009 10:52 am
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.