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
  • 10237 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 15, 2009

I know the writing to an external file doesn't store the data, that's why I commented that it was only to prove whether or not the php script is executed.  For now I was just formatting a full name from the elements, which will ultimately be placed at the top of succeeding forms; most likely via session variables, unless there's a better way.  Once I can get everything working, I'll also add the code to store the posted information (there're really more fields than just a name in the form) into a MySQL database for use in other pages and for other reasons.  I'm just learning this, so I wanted it to be K.I.S.S until it works and I understand it.

Beyond that, please tell me what other coding errors are there

David_Powers
Inspiring
September 15, 2009

Beyond that, please tell me what other coding errors are there

I highlighted them in red for you in my previous reply.