PHP_SELF

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
Phactor
Forum Newbie
Posts: 1
Joined: Mon Dec 06, 2010 9:58 pm

PHP_SELF

Post by Phactor »

Just a general question regarding the use of the PHP_SELF variable in forms. According to most examples I have seen (ex: http://www.html-form-guide.com/php-form ... -self.html), if you use the php self variable in the action of a form, you need an if statement before the form. For example:

Code: Select all

<?php
if(isset($_POST['submit'])) 
{ 
    $name = $_POST['name'];
    echo "User Has submitted the form and entered this name : <b> $name </b>";
    echo "<br>You can use the following form again to enter a new name."; 
}
?>
<HTML>
<HEAD><title>Using PHP_SELF</title></HEAD>
<BODY> 
<FORM method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
   <input type="text" name="name"><br>
   <input type="submit" name="submit" value="Submit Form"><br>
</FORM>
</BODY>
</HTML>

Why is it necessary to check if the form has already been submitted and why won't the form work without this check (if statement)?
Thanks
matt1234
Forum Commoner
Posts: 44
Joined: Wed Nov 26, 2008 9:43 pm

Re: PHP_SELF

Post by matt1234 »

It depends on what you're trying to do. For instance, I've had a page before that is where the form gets submitted to and if someone hits that page but didn't submit a form, it would take them to the homepage. If the form was submitted on its way to that page, a thank you message would be displayed. The IF statement before the form has nothing to do with PHP_SELF. That variable simply holds the value of the name of the page you're on. In the case of the example you showed, it's not necessary to have that IF statement to check if the form was submitted unless you wanted it to say
User Has submitted the form and entered this name : (Name)
You can use the following form again to enter a new name.
at the top of the page. That and the PHP_SELF call are completely separate.
Post Reply