Jump to content

Include based on variable Help


Kadage
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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;
	}
         }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.