executing a php file within another php file with include()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
whkowiak
Forum Newbie
Posts: 8
Joined: Tue Dec 14, 2010 3:44 am

executing a php file within another php file with include()

Post by whkowiak »

For future reference, please enclose PHP code within tags {syntax=php ] and [/syntax ] (without the spaces) to make your post readable, as I have done for you below. Use the "PHP Code" button at the top of the box where you are entering your post. Thank you.
Hello there,

I am learning slowly to create dynamic pages with php and I have a page called index.php in which I want to include the content of another page (called php.php).
It works well so far, I have the content of php.php displayed but then is my problem.
The file php.php contains a piece of (basic) php code that splits a word in 2 and when I execute it (it basically works using a GET function), I have the result displayed on the page generated by php.php (I think because the action is precisely php.php) and not inside my index.php page.
Here are the codes for the index.php:

Code: Select all

<html>
	<table width='70%' align='center'>
		<tr>
			<td>
			<h1>My web page</h1>
			</td>
		</tr>
	</table>
	<table width='70%' align='center'>
		<tr>
			<td width='20%'>
			<a href='index.php'>Home</a><br>
			<a href='index.php?page=newpage'>Tutorials</a>
			</td>
			<td width='80%'>		
				<?php
					if (!isset($_GET['page'])) 
					{
					include('php.php');
					}
					else
					{
					$page = $_GET['page'];
					include("inc/".$page.".php");	
					}
				?>
			</td>
		</tr>
	</table>
</html>
and the one of php.php:

Code: Select all

<html>
<form action="php.php" method="GET">
	<input type="text" name="fullname">
	<input type="submit" value="Split">
</form>

<?php

$firstname = "";
$lastname = "";

if(!isset($_GET['fullname']))
{
$_GET['fullname'] = "undefine";
}
else
{
$fullname = $_GET['fullname'];

$firstnamedone = false;

$len = strlen($fullname); 

if ($fullname)
{
for ($x=0;$x<=$len;$x++)
 {
    if (mb_substr($fullname,$x,1)!=" " && $firstnamedone == false)
	{
		$firstname = $firstname.mb_substr($fullname,$x,1);
	}
    else
	{
		$firstnamedone = true;
		$lastname = $lastname.mb_substr($fullname,$x,1);
	}
 }

$lastname = substr($lastname,1,($len)-1);

echo "
<input type='text' value='$firstname'>&nbsp;
<input type='text' value='$lastname'>";
}
else
{
echo "Please enter a name, dog.";
}
}
?>
</html>
I don´t know how to have the php.php executing itself inside the index.php page.
Last edited by califdon on Sat Dec 18, 2010 11:46 am, edited 1 time in total.
Reason: Added syntax tags.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: executing a php file within another php file with includ

Post by califdon »

You can't do it that way. The way include() works is to import the contents of a file into a PHP script file in which it just becomes a part of that file, for all practical purposes. You can't use the included file as a separate script, such as using it as the action= parameter of a form. Your logic must happen in the main script, something more like this:

Code: Select all

<?php 
if (!isset($_GET['page']))  {
   $page = 0;
} else {
   $page = $_GET['page']; 
}
?>
     <table width='70%' align='center'>
          <tr>
               <td>
               <h1>My web page</h1>
               </td>
          </tr>
     </table>
     <table width='70%' align='center'>
          <tr>
               <td width='20%'>
               <a href='index.php'>Home</a><br>
               <a href='index.php?page=newpage'>Tutorials</a>
               </td>
               <td width='80%'>               
<?php
if ($page == 0)  {
   include('php.php');
} else {
   include("inc/".$page.".php");  
}
?>
              </td>
          </tr>
     </table>
</html>
Then your "php.php" file (a poor name, if you ever expect to write more scripts) would merely be the content you want to insert when no $_GET parameter is given.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: executing a php file within another php file with includ

Post by social_experiment »

PHP Manual wrote:The include() statement includes and evaluates the specified file
Your file (php.php) contains a form that works with $_GET so it needs to submitted somehow.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
whkowiak
Forum Newbie
Posts: 8
Joined: Tue Dec 14, 2010 3:44 am

Re: executing a php file within another php file with includ

Post by whkowiak »

Thank you both for your answers and sorry for the look of the code, I´ll use the appropriate tags in the future.
As for the name of the file, I clearly made a poor choice as I was just intending to get the thing done, as an example, just to practice and learn php.
Thank you for your help!
Post Reply