Hi, I already looked all over the web on this and still don't quite understand it.

From what I can see $_SERVER['PHP_SELF'] is supposed to keep the same page in the browser when a form sends. If you use it in the form action= area.

What I don't understand is usually in a form action you have to connect with what else you need the form to do. Such as sending form fields to something else. A database or an email or something. So if $_SERVER['PHP_SELF']
takes up the form action slot, how can you send the form fields?

Thanks a lot.

    You would only use $SERVER['PHP_SELF'] if, in fact, you want the form to call the same page. You don't have to do it, and you can instead have it call any other file or URL that you'd like. But (there always has to be a "but", right?), you can have one PHP file fulfill 2 or more operations via the use of various conditional statements. As a simple for-instance:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang='en'>
    <head>
    <meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
    <title>Sample Page</title>
    </head>
    <body>
    <?php
    if(isset($_POST['sample']))
    {
       echo "<p>You entered '".htmlspecialchars($_POST['sample'])."'.</p>\n";
       echo "<p><a href='{$_SERVER['PHP_SELF']}'>Back to form</a></p>\n";
    }
    else
    {
    // display the form:
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>Enter something:
    <input type="text" name="sample" size="20" maxlength="20">
    <input type="submit" value="Enter">
    </p>
    </form>
    <?php
    }
    ?>
    </body>
    </html>
    

      Oh man, I'm glad to talk to someone that knows this stuff who explains it so well. What a great example that was.
      What does this do, I've seen other people use it.
      <?php
      }
      ?>

      I just realized, it all seems to be on the same page still even though the form has disappeared. At least the browser says it is. If someone can tell me how to show another exact form on the same page without clicking on the form link to get one, my problem will be solved.

      Thank you very, very, much.

        I think I got it. Thanks NOG!! Everybody, if you need anything with php, ask nog!

        <?php
        if($POST['text']=="")
        {
        echo "<form method=\"post\" action=\"{$
        SERVER['PHP_SELF']}\">";
        echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";
        }
        else
        {
        echo $POST['text'];
        echo "<form method=\"post\" action=\"{$
        SERVER['PHP_SELF']}\">";
        echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";

         }

        ?>

          To answer your previous question:

          Jazzy_Girl wrote:

          What does this do, I've seen other people use it.
          <?php
          }
          ?>

          It's simply ending his if() statement. One of the nice things about PHP is that you can easily switch from PHP code to pure HTML. In otherwords, doing this:

          <?php
          if($_POST['text']=="")
          {
          echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
          echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";
          }

          can be rewritten as this:

          <?php
          if($_POST['text']=="")
          {
          ?><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
          <p><input type="text" name="text"><p><input type="submit" value="submit"></p></form>
          <?php
          }

          Also, one thing about your code... if you're trying to use if($_POST['text']=="") to check if the form has been submitted or not, I would recommend using either [man]isset/man or [man]empty/man... otherwise, you're generating an E_NOTICE "undefined index" error every time.

            Thanks! Put it in notepad, you'll see its not checking for anything, but listing the text entered.

              Jazzy Girl wrote:

              Thanks! Put it in notepad, you'll see its not checking for anything, but listing the text entered.

              you are misunderstanding what brad is saying...

              sometimes, just checking to see if it is == "" doesn't work as it may send a blank space through the form therefore making it != "" plus will generate a NOTICE

              so checking with isset/empty/or a combo would be better...

              the NOTICE he is talking about will not necessarily show up, depending on your error reporting, but it is still generating a NOTICE

              hth,
              stolzyboy

                no, it won't "harm" anything... it's typically just bad practice, it's the best practice to make it so no warnings/notices/etc... show up (even if they don't actually show up)

                but it's not going to likely change your results unless the php reporting defaults change, then your users will start seeing the notices and such...

                stolzyboy

                  Thanks, its not that I am not concerned about it, its just that do to time limits I only want to investigate further when I have time.

                  Guess what. Someone gave me this, maybe you guys who were so nice to help me maybe you can use it too. This does the same thing as the php self without the php self. Who knew that if you left the form action blank like this, it would post on the same page anyway. action="" I have been messing with trying to code the php self just so for a few days. This stops all of it. Can you guys see any reason why I shouldn't use it? But whats also nice about the following is that the form fields not only appear on the page it sends to an email as well which is what I was looking for.

                  <?php
                  if($_POST['text']=="")
                  {
                  echo'
                  <form method="post" action=""><input type="text" name="text"><br /><br /><input type="submit" value="submit"></form>
                  ';
                  }
                  else
                  {
                  echo $_POST['text'];
                  $mailto = 'emailaddress@website.com';
                  $sendfield = $_POST['text'];
                  $subject = 'Email subject';
                  $from = "From: ".$sendfield. "\n" ;
                  $messageproper = 'Field sent to this email: '.$sendfield.'\n';
                  mail($mailto,$subject,$messageproper,$from);
                  
                  echo'
                  email sent';
                  }
                  ?>

                  So now I only need help with one small thing from someone please, I'll really appreciate it. I am trying to use the just above code that carries an item over from a database. Normally it looks something like this and works with the usual database items above it.

                  $query = "SELECT websitename FROM checkname WHERE websitename = '$modify'";
                  $result = mysql_query($query)  or die('Error with query' . mysql_error());
                  if (mysql_num_rows($result) == 0)
                  {
                  echo "$modify not found, please recheck name.
                  Just click the 'back' button to try again.";
                  }
                  while ($row = mysql_fetch_array($result)) 
                  {
                  
                  echo"<form action="" method='post'>

                  etc .

                  But now that I want to use the just above new code after the database info, I added it like this.

                  $query = "SELECT websitename FROM addscript WHERE websitename = '$modify'";
                  $result = mysql_query($query)  or die('Error with query' . mysql_error());
                  if (mysql_num_rows($result) == 0)
                  {
                  echo "$modify not found, please recheck name.
                  Just click the 'back' button to try again.";
                  }
                  if  
                  ($_POST['text']=="") { echo ' <form method="post" action=""><input type="text" name="text"><br /><br /><input type="submit" value="submit"></form> '; } else { echo $_POST['text']; $mailto = 'emailaddress@website.com'; $sendfield = $_POST['text']; $subject = 'Email subject'; $from = "From: ".$sendfield. "\n" ; $messageproper = 'Field sent to this email: '.$sendfield.'\n'; mail($mailto,$subject,$messageproper,$from); echo' email sent '; }} ?>

                  If you notice in order to accomodate the new code I changed this

                  while ($row = mysql_fetch_array($result)) 
                  {}

                  To this

                  while  ($row = mysql_fetch_array($result)){
                  if($_POST['text']==""){
                  echo '

                  I put the if INSIDE the "while" statement, which seems like it should be the proper way but tell me if I'm wrong.

                  But now when text is entered I get 5 results on the page and to the email instead of only one.

                  When the form works all alone it doesn't do this, but when I add it after the database info it duplicates. Need it to be only one. Can someone see the duplication erro? I can't see it.
                  Help is appreciated. Thank you very much.

                    Read my signature to see about using BB Code in the forums, it is the first link and it might help if you edit your post to actually use them

                      OK I'll try on my last post, seems pretty easy. Looks like what I am doing wrong is filling out quick reply instead of regular reply which doesn't have all those options in it.

                      Help is still appreciated on my duplication problem.

                        Jazzy Girl wrote:

                        is filling out quick reply instead of regular reply which doesn't have all those options in it.

                        It does - you just have to type them by hand.

                          I got it. You have to put Distinct in the query to stop the duplication.
                          $query = "SELECT DISTINCT websitename FROM checkname WHERE websitename = '$modify'";

                            Write a Reply...