Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 8th, 2004, 11:28 AM
Authorized User
 
Join Date: Jun 2003
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP_SELF

Hello there,


I am trying to really understand PHP_SELF and how to utilize it.. I am trying to display the firstname and lastname of this form but unfortunately I am unable to do so... Can you please assist me...


<html>
<h2>Simple Form Example</h2>

    <? function show_form($first="",$last="") { ?>

        <form action="<? $_SERVER['PHP_SELF']?>" method="post">
                First Name:
                <input type=text name=first value="<?echo $first?>">
                <br>
                Last Name:
                <input type=text name=last value="<?echo $last?>">
                <br>
                <input type=submit>
                </form>

        <? } ?>

        <?

        if( !isset($first)) {
                show_form();

        }
        else {
            if(empty($first) || empty($last)) {
                echo "Please fill in fields";
                show_form($first, $last);

                 }
                 else {
                     echo "Thank you, ". $_GET['$first'.' $last'];
                 }
        }
        ?>



</html>


I kow it can be tottaly wrong, but I want the entered data to display on the same page in the bottom of the form...


Thanks
 
Old April 8th, 2004, 11:38 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Use $_POST.

http://www.php.net/manual/en/reserve...variables.post

HTH,

Snib

P2P Member
<><
 
Old April 9th, 2004, 04:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you look at the code that actually uses $_SERVER['PHP_SELF'], you'll notice the there's a very important keyword missing...


echo


You never actually output the value of PHP_SELF to the client, so the name is missing from the form.


Also, as Snib mentioned, you should use $_POST to access form data submitted via a POST form. You access these variables using their global names (e.g. $first), which don't exist if register_globals is off, and via $_GET, which doesn't exist because your form submits via POST.

That said, you should clean up your code. For example:
   if (!isset($first))

should be
   if (!isset($_POST['first']))


And
    echo "Thank you, ". $_GET['$first'.' $last'];

Should actually be

    echo "Thank you, " . $_POST['$first'] . $_POST['$last'];


Notice in this last example you had concatenated two strings to create a single array index, when what you intended to do was concatenate the values stored at two different array indexes.

Take a minute to reread your original version. PHP would concatenate the strings and create this:

    echo "Thank you, ". $_GET['$first $last'];

Can you see why this is incorrect? You want to access the index 'first' of the array. Here, 'first' and 'last' are not variables, they're just indexes into an array. The value is accessed via the array variable, which is why $_GET has a dollar-sign in front of it. $_GET is the variable, an array. 'first' and 'last' are indexes into that array.

If my change does not make sense, let me know and I'll go into more detail.


Take care,

Nik
http://www.bigaction.org/
 
Old April 12th, 2004, 11:35 AM
Authorized User
 
Join Date: Jun 2003
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you all for your assistance... I looked and I fixed based on your recommendations, thanks again...

Dung





Similar Threads
Thread Thread Starter Forum Replies Last Post
error HREF=$PHP_SELF?action broccolo PHP Databases 5 December 26th, 2004 10:07 AM
error in PHP_SELF?action= broccolo PHP Databases 1 November 23rd, 2004 10:47 AM
error in <form action='{$_SERVER['PHP_SELF']} lanita PHP How-To 1 July 27th, 2004 01:07 AM
$PHP_SELF pb7 Beginning PHP 1 September 9th, 2003 11:28 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.