Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/142254-php_self-from-an-included-file/
Share on other sites

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.

</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']){

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.