SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Grimsby
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passed Variables od not show

    I've recently installed PHP on Mac OS X using the info provided at: http://www.entropy.ch/software/macosx/php/

    I'm currently going through Kevin Yanks tutorials before deciding to buy the book.

    I've only got as far as passing variables from page to page but it doesn't display. I'm not using open file in Internet Explorer but my IP address:

    http://213.105.28.16/~dlogic/tutorial/welcome.htm

    I presume its something to do with my installation of PHP as it works on my webspace:

    http://www.dimorphism.co.uk/welcome.htm

    The code for welcome.htm is:

    PHP Code:
    <html>
    <
    head>
    <
    title>Welcome</title>
    </
    head>
    <
    body>
    <
    form action="welcome.php" method="post">
    First Name: <input type="text" name="firstname" /><br />
    Last Name: <input type="text" name="lastname" /><br />
    <
    input type="submit" value="GO" />
    </
    form>
    </
    body>
    </
    html
    and the code for welcome.php is:

    PHP Code:
    <?php
      
    echo( "Welcome to our Web site, $firstname $lastname!" );
    ?>
    I hope that makes sense, any help greatly appreciated.

  2. #2
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Due to some security concerns a modification to PHP was made which now means that variables passed via the POST and GET method are no longer automatically available to you globally, thus the syntax you're using won't work right out of the box.

    Access your POST and GET variables like this:
    PHP Code:
    $_POST['firstname']
    $_GET['firstname'

    PHP, 4.2.0 (?) and later will have register_global=off as the default setting, thus requiring you to access them via the syntax noted above. What the developers have done is build some new arrays into PHP: $_POST[] and $_GET[] being just 2 of the new arrays.

    You can modify your php.ini file to have register_globals=on, then the scripts you're building from Kevin's book will work.

    It is advised, however, that you don't do this and simply use the new syntax.

    HTH

    Pete


  3. #3
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Grimsby
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    I've modified welcome.php:

    PHP Code:
    <?php
      
    echo( "Welcome to our Web site, $_POST['firstname']" );
    ?>
    but get the following error:

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /Users/dlogic/Sites/tutorial/welcome.php on line 2

    Cheers,
    Darren.

  4. #4
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change this:
    PHP Code:
    echo( "Welcome to our Web site, $_POST['firstname']" ); 
    to
    PHP Code:
    echo( "Welcome to our Web site, " $_POST['firstname'] ); 
    Note the dot after the closing quotation and before the $_POST['firstname'] reference.

    This concatenates ( attaches ) the $_POST['firstname'] to the string.


  5. #5
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or alternatively, change it to
    PHP Code:
    echo( "Welcome to our Web site, {$_POST['firstname']}); 
    in curly brackets...which makes it clearer to php where the variable begins/ends afaik
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  6. #6
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, yeah, forgot abput that!

    i do have a preference for my way in that my editor will use highlight the PHP syntax correctly, thus it's easier to read within the editor.

    FYI: using either EditPlus2 or Homsite
    Last edited by phpPete; May 29, 2002 at 04:57.


  7. #7
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Grimsby
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Guys, its appreciated. I'll take a look at php.net before posting in future to check for depreciated or modified functions etc. e

  8. #8
    SitePoint Member
    Join Date
    Mar 2002
    Location
    Grimsby
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but can't get my head around this, similar trouble to before.

    PHP Code:
    <html>
    <head>
    <title>Sample Page</title>
    </head>
    <body>

    <?php if ( !isset($GET['name']) ): ?>

    <!-- No name as been provided, so we prompt for one. -->

    <form action="<?=$PHP_SELF?>" method"get">
    Please enter your name: <input type="text" name="name" />
    <input type="submit" value="GO" />
    </form>

    <?php else: ?>

    <p>Your name: <?=$name?></p>

    <p>This paragraph contains a
    <a href="newpage.php?name=<?urlencode($name)?>">link</a>
    that passes the name variable on to the next document.</p>

    <?php endif; ?>

    </body>
    </html>
    The page refreshes but always asking for a name even though one is attached to the URL.

    Cheers,
    Darren.

  9. #9
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a fix of your code:

    You had method="get" in form, you want method="POST". use get when getting a variable from a url.

    Also, you're link had improper syntax, look at the change.

    Also, and this will get you EVERY time, you had a semi colon after the If test. by doing that you end that bit of code at that point! you want to do the test, then put what you want php to do inside curly braces, called a block
    [php]
    if(!isset($_POST['name']))
    {// begin block


    PHP...Do something here..


    }//end block


    Compare your code against what I have.


    PHP Code:
    <?php
     
    if(!isset($_POST['name']) ) {

    //<!-- No name as been provided, so we prompt for one. -->
    ?>
    <form action="<?=$PHP_SELF?>" method="POST">
    Please enter your name: <input type="text" name="name" />
    <input type="submit" value="GO" />
    </form>
    <html>
    <head>
    <title>Sample Page</title>
    </head>
    <body>


    <?php 
    }
    else{
    ?>
    <p>Your name: <?=$_POST['name'?></p>

    <p>This paragraph contains a
    <a href="newpage.php?name=<?php echo(urlencode($_POST['name']))?>">link</a>
    that passes the name variable on to the next document.</p>
    <?php
    }
     
    ?>

    </body>
    </html>
    This would also work, closer to what you had.
    <?=urlencode($_POST['name'])?>
    Last edited by phpPete; May 29, 2002 at 06:57.


  10. #10
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    method="get" is actually valid as well for forms...provided that you're not passing huge amounts of data (e.g. a whole multiline text field or more than 3-4 short variables).
    i think the problem might just have been that your code had method"get", without the equal sign...
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com


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
  •