Skip to main content
Known Participant
September 15, 2009
Answered

Why doesn't "$_SERVER['PHP_SELF']" in action= work for me.

  • September 15, 2009
  • 1 reply
  • 10236 views

I'm trying to make a form "self post", and was referred to the explanation at www.html-form-guide.com.  I coded my page exactly as shown there, and even copied/pasted the action= code (simple as it is) to my form tag.

In my XAMPP test environment the submit yields:

     Access forbidden!

          You don't have permission to access the requested object.  It is either read-protected or not readable by the server.

          If you think this is a server error, please contact the webmaster     (who would he be in a localhost environment?)

     Error 403

When I tested on my Yahoo web hosting server, submit yieded:

     The web page cannot be found.

          The most likely causes are:

               etc. etc.

Here's the code;

The following is placed at the top of the page immediately before the <html> tag:

<?php

     // the following was to write the post info to a file to verify that this code was being executed - never happened.

     $out = $_SERVER['PHP_SELF'];

     $fho = fopen(C:\xampp\htdocs\iod\testphpself.txt,wt);

               fwrite($fho,$out);

               fclose($fho);

     if (isset($_POST['submit']))

          $fstname   = $_POST['firstname'];

          $midinit     = $_POST['midinitial'];

          $lastname = $_POST['lastname'];

          $milen       = (strlen($midinit));

          if ($milen == 0)

               {$fullname = $fstname." ".$lastname;}

          else

               {$fullname = $fstname." ".$midinit". ".$lastname;}

          echo "Information has been stored for : $fullname <br /><br />";

The following is the <form> code:

     <form name="form1" method="post" action="<?php echo $SERVER['PHP_SELF']; ?>">

Most of the above I gleaned from www.html-form-guied.com/php-form/php-form-action-self.html.

PLEASE! Can someone tell me where my error(s) is/are?  I feel that I must be doing something wrong, but have no idea what it might be.

This topic has been closed for replies.
Correct answer David_Powers

creacontech wrote:

I can't figure out how to cut/paste the code in here

There's an announcement at the top of the forum that tells you how to display the Useful Links box, where you'll find, well... a lot of useful links, including Posting code in this forum.

For some reason, the if statement does not become true when the submit button is pressed, so the script is never executed.

The if statement checks for $_POST['submit']. Does it exist? If the statement isn't true, even when the form is submitted, it means your submit button has a different name. You have posted only the processing code, and not the form, so I can't tell what your submit button is called, but that's where the problem lies.

1 reply

David_Powers
Inspiring
September 15, 2009

creacontech wrote:

          If you think this is a server error, please contact the webmaster     (who would he be in a localhost environment?)

You.

I don't really understand what you're trying to do, but there are several mistakes in your PHP code.

<?php

     $out = $_SERVER['PHP_SELF'];

     $fho = fopen('testphpself.txt','wt');

               fwrite($fho,$out);

               fclose($fho);

     if (isset($_POST['submit'])) {

          $fstname   = $_POST['firstname'];

          $midinit     = $_POST['midinitial'];

          $lastname = $_POST['lastname'];

          $milen       = (strlen($midinit));

          if ($milen == 0)

               {$fullname = $fstname." ".$lastname;}

          else

               {$fullname = $fstname." ".$midinit . " ".$lastname;}

          echo "Information has been stored for : $fullname <br /><br />";
     }
?>

What that does is write the name of the current page to the external text file. It does not store the form content.

Known Participant
September 16, 2009

I can't figure out how to cut/paste the code in here, so I pasted it in a wordpad file which is attached (as soon as I changed the file extension to .php, things started to work).  I changed the code somewhat, and made the corrections you showed me, but now there's a different problem.  For some reason, the if statement does not become true when the submit button is pressed, so the script is never executed.  If I comment out the if statement, the script executes twice, as would be expected; i.e. once when the page is first opened, and once when the form is submitted.  I've checked it over and over and compared it to equivalent example code in W. Jason Gilmore's book, and I can't identify where my error is.  Could you look at it and tell me what I've done wrong now?

BTW, is there not a way to cut/paste code snipets here from my page(s)?  I can't seem to figure out how to do that.

David_Powers
David_PowersCorrect answer
Inspiring
September 16, 2009

creacontech wrote:

I can't figure out how to cut/paste the code in here

There's an announcement at the top of the forum that tells you how to display the Useful Links box, where you'll find, well... a lot of useful links, including Posting code in this forum.

For some reason, the if statement does not become true when the submit button is pressed, so the script is never executed.

The if statement checks for $_POST['submit']. Does it exist? If the statement isn't true, even when the form is submitted, it means your submit button has a different name. You have posted only the processing code, and not the form, so I can't tell what your submit button is called, but that's where the problem lies.