eelmitchell Posted March 22, 2009 Share Posted March 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
jackpf Posted March 22, 2009 Share Posted March 22, 2009 Uhh, I think $_SERVER[] is the superglobal you're looking for... And anyway, you can just put form action="" and it'll call itself. Quote Link to comment Share on other sites More sharing options...
eelmitchell Posted March 23, 2009 Author Share Posted March 23, 2009 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.