SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member melbee33's Avatar
    Join Date
    Mar 2002
    Location
    California
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie Question: Undefined Index

    I just downloaded the Sitepoint PHP/MySql Book and am trying out some of the code. I get this error:
    Notice: Undefined index: tname2 in c:\inetpub\wwwroot\test2.php on line 15

    from this code if I do not pass a variable:

    <?php
    echo ("<a href="test2.php?tname=melissa">Melissa</a>");
    $tname=$_REQUEST['tname'];
    echo("this is the name you clicked on: $tname");
    ?>

    so I added:

    <?php
    echo ("<a href="test2.php?tname=melissa">Melissa</a>");
    var $tname;
    $tname=$_REQUEST['tname'];
    echo("this is the name you clicked on: $tname");
    ?>

    which now gives this error:

    Parse error: parse error, unexpected T_VAR in c:\inetpub\wwwroot\test2.php on line 13

    Am I doing something wrong? I have this code within the body tags/html and php is running on IIS.

    Any help would be much appreciated!

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the same problem I've been having lately. I'm guessing it's something to do with the PHP config on the server (which also runs Windows and IIS). However a quick workaround is as follows:
    PHP Code:
    if(isset($_REQUEST['tname'])) {
      
    $tname $_REQUEST['tname'];

    And this seems to work for me.

    Hope this helps!

    -Sam
    Sam Hastings

  3. #3
    SitePoint Member melbee33's Avatar
    Join Date
    Mar 2002
    Location
    California
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works if I remove the var statement and only if the query string is in the url (?tname=whatever). I'm baffled.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2003
    Location
    Singapore
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    echo ("<a href="test2.php?tname=melissa">Melissa</a>" );
    var 
    $tname;
    $tname=$_REQUEST['tname'];
    echo(
    "this is the name you clicked on: $tname);
    ?>
    Not sure if it solves the prob but the " should be escaped if I'm not wrong. I'm not that good at noticing which ones need escaping.

    PHP Code:
    echo ("<a href=\"test2.php?tname=melissa\">Melissa</a>" ); 

  5. #5
    SitePoint Member melbee33's Avatar
    Join Date
    Mar 2002
    Location
    California
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. That's not actually where I'm having the problem but that's a neat trick. I normally just use single quotes within the href tag and that seems to work too. Like I said, I'm very new at this. I'm pretty sure now that it has to do with the Windows Server. I've tested it on a Linux box (Apache) and it does not show the error. The problem is in using the $_REQUEST without passing any variable until after someone does something... or if there is no variable passed to just allow it to be empty.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can continue on Sam's code:
    PHP Code:
    if(isset($_REQUEST['tname'])) {
        
    $tname $_REQUEST['tname'];
    } else {
        
    $tname '';

    Or shorther syntax:
    PHP Code:
    $tname = isset($_REQUEST['tname']) ? $_REQUEST['tname'] : ''
    HTH

    -Helge

  7. #7
    Ribbit... Eric.Coleman's Avatar
    Join Date
    Jun 2001
    Location
    In your basement
    Posts
    1,268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or you can use

    PHP Code:
    if( array_key_exists'tname'$_REQUEST ) )
    {
        
    $tname $_REQUEST['tname'];
    }
    else
    {
        
    $tname '';

    Eric Coleman
    We're consentratin' on fallin' apart
    We were contenders, now throwin' the fight
    I just wanna believe, I just wanna believe in us

  8. #8
    SitePoint Member melbee33's Avatar
    Join Date
    Mar 2002
    Location
    California
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile This works!

    The shorter version posted by Helge works very well to solve this problem.
    Code:
    $tname = isset($_REQUEST['tname']) ? $_REQUEST['tname'] : ''; 
    Thank you all for your help. I will continue on my learning adventure now


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •