SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Form action?

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    Cape Town, SA
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form action?

    I've currently got a form that has as the action:
    Code:
    action="<?php echo $_SERVER['PHP_SELF']; ?>
    I picked that up from some tutorial - but not enitrely sure what it does, and why I'm using it.

    Basically, I'm not wanting to post the form to anywhere really, but rather grabbing the form elements via $_POST['name'] etc. and making use of them further down the page.

    So it's working and all, just want to know why the tutorial suggested it, when it looks like I can even take the action bit out and it still works.

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, the author probably suggested it because it is easier to use that then to type the page name, I guess. You can change the filename and the form will work. Um.. That is quite stupid explanation, but I can't think of anything else.
    I personaly don't use it.
    $_SERVER['PHP_SELF'] contains the filename of the currently executing script.
    If the filename is test.php, there is no difference if you use
    <form action="test.php"> or
    <form action="<?php echo $_SERVER['PHP_SELF'];?>">
    -- Jelena --

  3. #3
    SitePoint Evangelist ldivinag's Avatar
    Join Date
    Jan 2005
    Location
    N37 33* W122 3*
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i read this along time ago in a book.

    the script page has the form and at the top, it checks if the fields are filled.

    if they are, then it runs that part of the script to authenticate the user (the script is a login screen).

    if the 2 fields (username AND password) were empty, then it displays the form.

    and the action is ITSELF...

    tricky... (was just learning php then...) but eventually, i ran the script and understood how it works.

    PHP Code:
    if (!$username) && (!password)
    {
      ?>
      <P><FORM ACTION="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD=POST>
       <P>username:<INPUT TYPE=text NAME=username VALUE="" SIZE=30></P>
       
       <P>password:<INPUT TYPE=password NAME=password VALUE="" SIZE=30></P>
       
       <P><INPUT TYPE=submit NAME=Submit VALUE="Submit">
      </FORM></P>
     <?php
    }
    elseif
    {
      
    //authenticate userinfo
      
    .
      .
      .
      if (
    $authenticated)
      {  
       
    //  set session info
       
    .
       .
       .
       
    header ("welcome_user.htm");
      }
      elseif
      {
       
    //  display error message about bad userinfo
       
    .
       .
       .
      }
    }
    leo d.

  4. #4
    SitePoint Addict devil cat's Avatar
    Join Date
    Apr 2003
    Location
    Reno
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_SERVER['PHP_SELF'] is simply a variable that holds the name of the file that it is sitting in.

    The reason it is used is so that you can either: a) change the name of the file and not worry about it, b) use in an include (or equivalent file) and not worry about it when you use it in multiple pages.

    The reason that you can remove it and the form still goes to the same place, is that without an action location for the form to send the information to, all it can do is send the information to the page the form is sitting on. It is probably not proper HTML standards though.


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
  •