problem with $_SERVER['PHP_SELF'] in form action

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Lou_Boumian
Forum Newbie
Posts: 5
Joined: Fri Feb 20, 2004 2:19 pm

problem with $_SERVER['PHP_SELF'] in form action

Post by Lou_Boumian »

Hi!
On my remote host server, I can run successfully this:

<form action="$PHP_SELF" method="post">

which is nested inside a $HTML=<<<HTML section

But I just set up PHP5 running on Appache 2 on my Windows2000 local machine and the form does not get processed with that code (although the php file display correctly the blank form). Pressing submit just return the blank form. No processing occurs.

So, I have tried using:
<form action="$_SERVER['PHP_SELF']" method="post">
with varying the combination of " and ', to no avail.

I get the following error message:
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

The test file :
<?php
$t = 10;
echo $t, '---', $_SERVER["PHP_SELF"];
?>
works however perfectly locally.
but in this case, the code is not nested inside a $HTML=<<<HTML section

Any idea what to try?

Thanks!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Are you also running php 5 on your localserver? I'm not too familar with the syntax of php... try this

Code: Select all

<?php
echo $t, . "---" . $_SERVER["PHP_SELF"]; 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Are you also running php 5 on your localserver? I'm not too familar with the syntax of php... try this

Code: Select all

<?php
echo $t . "---" . $_SERVER["PHP_SELF"]; 
?>
srry for double post... gg isp
Last edited by John Cartwright on Fri Feb 20, 2004 3:54 pm, edited 1 time in total.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

can you post more of your actual code? such as the heredoc (and a few lines before it too)
User avatar
Lou_Boumian
Forum Newbie
Posts: 5
Joined: Fri Feb 20, 2004 2:19 pm

Post by Lou_Boumian »

here is a bit of my code:

<?php
global $bFirstPassage;
if (empty($bFirstPassage))# If first pass on page, display the form
{ ShowForm();
}
else #Checking for errors in input:
(...)

?>

(...)

<?php
// Definition of function ShowForm
function ShowForm() {
global $PHP_SELF;
$HTML=<<<HTML
<tr>
<td>
<form action="$PHP_SELF" method="post"> #form not processed

/*
<form action="$_SERVER['PHP_SELF']" method="post"> gives parsing error:Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

<form action="{$_SERVER["PHP_SELF"]}"method="post"> does not process the form

<form action="'.$_SERVER["PHP_SELF"].'" method="post"> gives same parsing error

*/

<input type="hidden" name="bFirstPassage" value="No">
<table>
<tr>
...
</form>
</td>
</tr>
HTML;
echo $HTML;

#Display warnings and copyrights:
include 'foot.html';

#Closing the HTML code:
echo "</table></td>
</tr>
</table>
</body>
</html>";
} # End of function ShowForm

?>
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

$PHP_SELF is only available when you have register_globals on in your php.ini file. You should use the $_SERVER global variable: eg: $_SERVER['PHP_SELF'].

Next since you will be echo'ing this inside of a heredoc you will need to {} the variable eg:

Code: Select all

<?php
echo <<<EOL
 Use { } with single quotes (possibly, never tried double quotes (")) to echo an array from inside a heredoc eg: {$_SERVER['PHP_SELF']} will echo the variable but $_SERVER['PHP_SELF'] will not 
EOL
That should do the trick for you
User avatar
Lou_Boumian
Forum Newbie
Posts: 5
Joined: Fri Feb 20, 2004 2:19 pm

Post by Lou_Boumian »

As mentioned in comment in the portion of the code I submitted, I have tried:
{$_SERVER['PHP_SELF']}

It does not trigger a parsing error but the form comes back instead of being processed, just like if reset. So, i wonder if there could be a problem with my $bFirstPassage variable. This variable should not be empty after the first display of the form. Hence, when the user submits it, the first lines of the code:

<?php
global $bFirstPassage;
if (empty($bFirstPassage))# If first pass on page, display the form
{ ShowForm();
}
else # If not first pass on page, check for invalid entries before displaying details entered by user:


should triggers processing rather than the ShowForm() function again.

Could that be a configuration problem somewhere, as it is the first PHP form I am trying to run locally on my machine (Again, this file works on a remote server)?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

if(isset($_POST['bFirstPassage'])){
process
}
else {
display form
}
User avatar
Lou_Boumian
Forum Newbie
Posts: 5
Joined: Fri Feb 20, 2004 2:19 pm

Post by Lou_Boumian »

You are right LiLpunkSkateR!

I actually realize that the problem was indeed that I was using $bFirstPassage instead of $_POST['bFirstPassage'] to test whether or not the file had been read once. Since I am using PHP5, $bFirstPassage was never recognized as the variable posted further done in the script and the test was therefore always returning FALSE, hence reshowing the blank form again and again.

I had to replace all my $PostedVariable by $_POST['PostedVariable'] to make my script work on my local machine.
I guess I could have also turn REGISTER_GLOBALS on in my config file and avoid changing my script but I read this is bad practice.

Eventually, I also had to use:
<form action={$_SERVER['PHP_SELF']} method="post">
to avoid parsing error within my heredoc.

Hope my beginner mistake will help others...!
Thanks for helping and for the bit of code with ISSET.
This forum rocks
Cheers!
Post Reply