Jump to content

[SOLVED] How can I return to a page via $_SESSION['PHP_SELF']


eelmitchell

Recommended Posts

I'm trying to get a form page to call back on itself by action=$SERVER['PHP_SELF"] but I don't believe the _SESSION variable is defined.

 

My test code page is:

 

<html> <body>
<h1>Test</h1>
<?php
    if($_POST) {
        echo("Return after form data entry<br />");
        $quantity = $_POST['quantity'];
    }
    else {
        echo("First time page opened.<br />");
?>
        <form method="post" action="<?php echo($_SESSION['PHP_SELF'])?>">
            Quantity:<input type="text" size="10" name="quantity"
                        value="<?php echo($_POST["quantity"])?>" />
        <input type="submit" name="Submit" value="Submit" />
        </form>
<?php
    }
?>
</body></html>

 

 

and the output is:

 

<html> <body>
<h1>Test</h1>
First time page opened.<br />        <form method="post" action="<br />
<b>Notice</b>:  Undefined variable: _SESSION in <b>C:\Inetpub\wwwroot\SalesForm\test.php</b> on line <b>11</b><br />
">
            Quantity:<input type="text" size="10" name="quantity"
                        value="<br />
<b>Notice</b>:  Undefined index:  quantity in <b>C:\Inetpub\wwwroot\SalesForm\test.php</b> on line <b>13</b><br />
" />
        <input type="submit" name="Submit" value="Submit" />
        </form>
</body></html>

 

The first time I would expect $_POST to be undefined and sure enough if($_POST) ... is false so we get to the <form ...> tag.

 

But the $_SESSION is unknown so the "action" field is filled with an error message and won't return to the page.  It should be http://localhost/salesform/test.php.

 

In addition the $_POST['quantity'] gives a message as an "Undefined index".  I know it's undefined but I would expect an empty string.  The second time through the value will be known and that makes the data sticky.

 

How can I get the page URL so the action can return and how can I produce the empty string for filling the form values the first time through?

 

Environment:

Windows XP Pro SP3

PHP 5.1 from binaries to C:\PHP5

php.ini is php.ini-recommended except for mysql changes.

 

Link to comment
Share on other sites

Thanks for correcting me.  $_SERVER['PHP_SELF'] turns into the correct file name for the page (action="/salesform/test.php">).

 

I also realised I shouldn't be getting the default value for the input text field from $_POST.  If I initialise $quantity to an empty string then the value is correct the first time in the form and will be overridden when the form returns with some data.

 

As a newcomer to PHP, I appreciate the help.

 

How do I put [sOLVED] on the Subject?  I read somewhere there was a button or do I just edit the subject line itself?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.