Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

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

New Here ,
Sep 15, 2009 Sep 15, 2009

Copy link to clipboard

Copied

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.

TOPICS
Server side applications

Views

5.9K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2009 Sep 15, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2009 Sep 15, 2009

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 15, 2009 Sep 15, 2009

Copy link to clipboard

Copied

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

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2009 Sep 15, 2009

Copy link to clipboard

Copied

Boy, have I got egg on my face big time!  Just pin the stupid badge on me and initiate me into the idiot club.  The WHOLE PROBLEM was that from ALL the research I did, I failed to pick up on the fact that the page had to have a .php extension instead of .html.  I renamed the file, and it's all working, save for my coding errors; at least the code is being executed.

Thanks everyone for your help.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2009 Sep 15, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

David_Powers wrote:

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.


I clicked on insert>Syntax Highlighting>Plain, and the box was inserted.  I then copied my form code from Dreamweaver, but couldn't find a "paste" any way to paste it.  Right clicking inside the box provided only choices to to format or remove; neither of which helped.  Couldn't identify a paste button anywhere.  Please tell me what the secret is.  I'm beginning to feel like a real dunce; all I need is the pointed hat.

More importantly, thank you for pointing me in the right direction - it was a matter of "case sensitivity".  The name= & id= were "Submit", and $_POST['submit'] was in lower case.  I changed the name and id to lower case and now it works correctly.

Man, did you ever nail that problem in a hurry.  I really have to wrap my head around this case sensitivity issue; I can't seem to remember to be careful about it - I guess that's senility kicking in.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

creacontech wrote:


Couldn't identify a paste button anywhere.  Please tell me what the secret is.

Ctrl+V or Cmd+V on a Mac. It's the common keyboard shortcut for paste in all programs.

Man, did you ever nail that problem in a hurry.  I really have to wrap my head around this case sensitivity issue; I can't seem to remember to be careful about it - I guess that's senility kicking in.

I doubt if it's senility. There are a lot of little things that catch you out when you're a beginner with PHP: case-sensitivity, mismatched quotes, brackets, and curly braces, missing semicolons. I remember only too well how painful it can be at times. But if you persevere, it should all fall into place quite quickly.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 16, 2009 Sep 16, 2009

Copy link to clipboard

Copied

LATEST

David_Powers wrote:

creacontech wrote:


Couldn't identify a paste button anywhere.  Please tell me what the secret is.

Ctrl+V or Cmd+V on a Mac. It's the common keyboard shortcut for paste in all programs.

Man, did you ever nail that problem in a hurry.  I really have to wrap my head around this case sensitivity issue; I can't seem to remember to be careful about it - I guess that's senility kicking in.

I doubt if it's senility. There are a lot of little things that catch you out when you're a beginner with PHP: case-sensitivity, mismatched quotes, brackets, and curly braces, missing semicolons. I remember only too well how painful it can be at times. But if you persevere, it should all fall into place quite quickly.

Well thanks again, and thanks for the encouragement, I really need it right now.  I was never aware of the Ctrl+v being a paste key.  Learn somthing new every day.  That I wrote down in my big black book, so I'll be sure to remember it.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines