Kadage Posted January 16, 2012 Share Posted January 16, 2012 I am developing a page that displays info from a database based on a variable called "ID". On this page is a form that when submitted runs an external function file to first error check the form and then insert it into the database. The problem i'm having is if i include the page which had to form to display the error message on, it doesn't load the right info from the database because its not loading based on the ID. So is there an alternative way to write: include ("toDo.php?ID=".$ToDoID); Any help is much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/ Share on other sites More sharing options...
AyKay47 Posted January 16, 2012 Share Posted January 16, 2012 have the form handling on the same page and pass the id from te form via the $_GET or $_POST methods. It would help if you posted the relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308160 Share on other sites More sharing options...
Kadage Posted January 16, 2012 Author Share Posted January 16, 2012 have the form handling on the same page and pass the id from te form via the $_GET or $_POST methods. It would help if you posted the relevant code. This isn't the exact code. But it shows exactly what I mean. I have a page that is displaying info based on results from a database. If the form fails to pass a check then i need it to include the previous page while getting the correct results. Include only lets me include the base file "Page.php" not "Page.php?ID=".$ID" thus resulting in no results being returned from the database. Page: $View = $_GET['ID']; $Query = "SELECT * FROM TBL_Clients WHERE ID = '$View'"; $Result = mysql_query($Query, $Host); while($R=mysql_fetch_array($Result)) { $ID = $R[iD]; $Name = $R[Name]; echo "<form action='Process.php?ID=".$ID."&Mode=New' method='post' name='New'> <b>Name:</b> <input name='Name' type='text' /> </form"; } Process: if($Mode == 'New') { $Name = $_POST['Name']; $ID = $_GET['ID']; if($Name == '') { $Error = "Insert Name"; include ("Page.php?ID=".$ID); die; } } Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308175 Share on other sites More sharing options...
PFMaBiSmAd Posted January 16, 2012 Share Posted January 16, 2012 When you include php code into another file (through the file system), the included code becomes part of the main file (at the point where it was included) and it has the exact same variable scope as the main file. Any variables that are present or that you set in the main file - $_GET['ID'], $ID, $ToDoID,... are already available to the included code. Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308177 Share on other sites More sharing options...
Kadage Posted January 16, 2012 Author Share Posted January 16, 2012 When you include php code into another file (through the file system), the included code becomes part of the main file (at the point where it was included) and it has the exact same variable scope as the main file. Any variables that are present or that you set in the main file - $_GET['ID'], $ID, $ToDoID,... are already available to the included code. Yes correct, they are. But the page displays different info based on the URL. E.g. http://www.example.com.au/admin/Page.php?ID=2 http://www.example.com.au/admin/Page.php?ID=1 Both of these pages display completely different info. When we include a file it only includes the Page.php. Because of that the code doesn't know which ID to load and so it doesn't load anything... Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308181 Share on other sites More sharing options...
PFMaBiSmAd Posted January 16, 2012 Share Posted January 16, 2012 If process.php uses include "page.php";, $_GET['ID'] will have the value in it that process.php was requested with and the page.php code will do what you expect. Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308183 Share on other sites More sharing options...
Kadage Posted January 16, 2012 Author Share Posted January 16, 2012 If process.php uses include "page.php";, $_GET['ID'] will have the value in it that process.php was requested with and the page.php code will do what you expect. You don't get what i'm trying to say. This is Page.php $View = $_GET['ID']; if($View == '1') { echo "Hello"; } if($View == '2') { echo "Goodybye"; } If Page.php is viewed like this: http://www.example.com.au/admin/Page.php Nothing can be displayed, because $View is not equal to 1 or 2. This is what include does. Include does not specify which ID i want loaded... Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308189 Share on other sites More sharing options...
scootstah Posted January 16, 2012 Share Posted January 16, 2012 I think it is you who doesn't get it. The included file has the same variable scope, thus you don't need to pass a GET to it... just use the variable itself. process.php $ID = $_GET['ID']; include ("Page.php"); Page.php echo $ID; Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308196 Share on other sites More sharing options...
PFMaBiSmAd Posted January 16, 2012 Share Posted January 16, 2012 If Page.php is viewed like this: http://www.example.com.au/admin/Page.php ^^^ But, that's NOT what you are doing where the problem occurs. You stated you are on the process.php?ID=123 page and you are including page.php into process.php to redisplay it and you want the 123 $_GET['ID'] value to be available in the page.php code. That's the answer you have been shown. At this point, I've got to ask, have you even looked at the php.net documentation for the include statement so that you know what it does? Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308206 Share on other sites More sharing options...
Kadage Posted January 16, 2012 Author Share Posted January 16, 2012 Yeah sorry about that. I was coding for hours and for the life of me couldn't see the what you guys were trying to say last night. Such a simple Fix i should have been able to realise. Once again sorry, and thanks heaps for your assistance guys. Quote Link to comment https://forums.phpfreaks.com/topic/255140-include-based-on-variable-help/#findComment-1308360 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.