landysaccount Posted January 24, 2009 Share Posted January 24, 2009 Hello. I currently have a problem with $_SERVER["PHP_SELF"] when called from within an included file: I have a several files included in one script, here are some snips of those files: ------------ head.php include( "./functions.php" ); include( "./constants.php" ); switch( $action ){ case "add": $file = "addclient.php"; break; case "view": $file = "viewclient.php"; break; default: $file = "cust_main.php"; } ---------- cust_main.php <form action="<? $_SERVER["PHP_SELF"] ?>"> <input type="hidden" name="action" value="add"> .... </form> <form action="<? $_SERVER["PHP_SELF"] ?>"> <input type="hidden" name="action" value="view"> .... </form> ----------- index.php include( "./head.php" ); <table> <tr><td><? include( "./top.php" ); ?></td></tr> <tr><td><? include( $file ); ?></td></tr> <!-- this file will dynamically change --> <tr><td><? include( "./bot.php" ); ?></td></tr> </table> ------------ The first time the script runs the default file loads fine but, when the submit button is pressed to send all the information to the server it seems that the head.php file doesn't get parsed or called only the file where the PHP_SELF was called. the variable $action never get changed. How can I work around this? Thanks in advanced for your help. EDIT: damn, I didn't even realize there were three files CODE tags are awesome btw Quote Link to comment https://forums.phpfreaks.com/topic/142254-php_self-from-an-included-file/ Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 index.php includes head.php which in turn includes cust_main.php. Since the main file being called is index.php, unless you use an absolute path, all path/to/files being included will be relative to index.php. But when you submit to cust_main using PHP_SELF, PHP_SELF is the path/to/file for cust_main.php itself, which breaks the relative path you had going. You can solve this by either using absolute paths for everything, or by doing action="path/to/index.php" in your form action, since it includes all those files anyway. If the form was on say, for instance, addclient.php, you would do action="path/to/index.php?action=add" in addclient's form. Quote Link to comment https://forums.phpfreaks.com/topic/142254-php_self-from-an-included-file/#findComment-745265 Share on other sites More sharing options...
Zane Posted January 24, 2009 Share Posted January 24, 2009 </pre> <form action="<?%20%24_SERVER%5B" php_self>">< You need to set your form's method....GET or POST by default it's going to be GET also....unless you have register globals on...this won't work switch( $action ){ should be switch($_GET['action']){ Quote Link to comment https://forums.phpfreaks.com/topic/142254-php_self-from-an-included-file/#findComment-745266 Share on other sites More sharing options...
landysaccount Posted January 24, 2009 Author Share Posted January 24, 2009 Ok. Now I seen the problem. I wasnt' using $_REQUEST["action"] in the switch control. Thank you for your help. Quote Link to comment https://forums.phpfreaks.com/topic/142254-php_self-from-an-included-file/#findComment-745272 Share on other sites More sharing options...
Maq Posted January 24, 2009 Share Posted January 24, 2009 And by the way, you shouldn't use short tags anymore, you should use "<?php". Quote Link to comment https://forums.phpfreaks.com/topic/142254-php_self-from-an-included-file/#findComment-745316 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.