php displaying stuff from another file

Joined
12 Feb 2006
Posts
17,643
Location
Surrey
im looking for a way that php will load everything that is on one file and then display it on the page? e.g. i have a php page called submit.php that has a form that will process and add the info from the form to mysql, i then want a simple code that i put on index.php that will just load everything that you would see looking at submit.php.

i have looked on php website and this seems to be kinda of what i'm after but i really don't understand it too well or know how to implement it as im still a newboy to php. Any be able to explain, and if thats not what im looking for, any way i can do this?
 
Yeah, include(); is the best way of doing it. Or require(); for that matter :) They are almost identical except in the way they handle errors- require(); will produce a fatal error if the file is missing and stop the script whereas include(); will produce a warning but continue the script. :)
 
Trigger said:
Yeah, include(); is the best way of doing it. Or require(); for that matter :) They are almost identical except in the way they handle errors- require(); will produce a fatal error if the file is missing and stop the script whereas include(); will produce a warning but continue the script. :)

i tired include but as i thought, it didnt load what was on submit.php and show it on index.php, it just refers to it.
 
addy_010 said:
i tired include but as i thought, it didnt load what was on submit.php and show it on index.php, it just refers to it.

Yes it does, i'm using it at the moment for a project i'm working on. Post your code.
 
ok here is what i put on index.php

Code:
<? include("submit/submit.php"); ?>

and here is submit.php
Code:
<? include("../login/include/session.php"); ?>

<?php

$file_name = $HTTP_POST_FILES['ufile']['name'];

$random_digit=rand(0000,9999);

$limit_size=50000;

$new_file_name=$random_digit.$file_name;

$file_size=$HTTP_POST_FILES['ufile']['size'];

$path= "upload/".$new_file_name;

if($ufile !=none)



if($file_size >= $limit_size){
echo "Your file size is over limit<BR>";
echo "Your file size = ".$file_size;
echo " K";
echo "<BR>File size limit = 50000 k";
}
else {

{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "File Upload Success<br />";

echo "<b>File Name :</b>".$new_file_name."<br />";
echo "<b>File Size :</b>".$HTTP_POST_FILES['ufile']['size']."<br />";
echo "<b>File Type :</b>".$HTTP_POST_FILES['ufile']['type']."<br />";
}
else
{
echo "";
}
}


include"connect.php"; 

if(!isset($_POST['add_news']))
	{
echo"
<br />
<br />
<form action='$self?action=addnews' name='addnews' id='form1' method='post' enctype=\"multipart/form-data\">
<table>

	<tr>
		<td>
			Title: <input type='text' name='title'>
		</td>
	</tr>

	<tr>
		<td>
			Description:<textarea cols='15' name='description' rows='5'></textarea>
		</td>
	</tr>

	<tr>
		<td>
			URL: http:// <input type='text' name='url'></input>
		</td>
	</tr>
	
	<tr>
		<td>
			Upload Image: <input type='file' name='ufile' id='ufile' size='50' /></input>
		</td>
	</tr>
	
	<tr>
		<td>
			<input type='submit' name='add_news' value='submit news topic'>
		</td>
	</tr>
	
</table>
</form>";	
}
elseif(isset($_POST['add_news'])) 
{
$title = mysql_real_escape_string($_POST['title']);
$url = mysql_real_escape_string($_POST['url']);
$description = $_POST['description'];
$date = date("d.m.y");
$username = $_SESSION['username'];
$imagename = $new_file_name;

$error = array();



if(empty($title)) {
$error[] = "You must enter a title<br />";
}
if(empty($description)) {
$error[] = "Enter a description please<br />"; 
}
if(empty($url)) {
$error[] = "The URL is needed<br />"; 
}

if(count($error)>0) {
echo"<font size='3' color='#CC0000'><strong>ERROR:</strong></font>";
foreach($error as $error2);
echo"$error2";
}
else {

$sql = "INSERT INTO submit(title, description, url, author, date, imagename, valid) VALUES('$title', '$description', '$url', '$username', '$date', '$imagename', '0')";

mysql_query($sql);
echo"Website successfully queued for verification. Your site will be added soon when verified by an Admin.<br />An email will be sent if there is any problems.";
}

}
}

?>

didn't work for me
 
Well firstly I can't see why you're using a combination of <? and <?php. It's bad practice- just use <?php. On index.php, is the <? include('submit.php') ?> between another set of <?php tags?- that could be your problem.

EDIT: Is it just that include() that doesn't work or all of them?
 
ok some odd reason now its working, well almost. the form and other infomration from submit.php is included, but the troulbe is that as some of the php in submit.php refers to directories that are now different to where index.php is.

How would i sort this? could i have it just select the form? so that that way it will still be submitted to submit.php.

why is it bad pratice to put <? and <?php? do they mean differnt things? the reason i have used them is because i was using tutorials to write the php and some would say ok start off with one and others would start with the other

also wanting to try out yahoo ads on my site, however i can't seem to find a way to get them. can someone post me a link to set them up for my site. i honestly have searched, and i can't find them, just yahoo financing and some other things
 
addy_010 said:
ok some odd reason now its working, well almost. the form and other infomration from submit.php is included, but the troulbe is that as some of the php in submit.php refers to directories that are now different to where index.php is.

The only way I can see it working then is if submit.php and index.php are in the same directories. Don't take that as the only answer though as I am by no means experienced in PHP. You could look at this and select which lines you want possibly but I don't know whether it's safe and there are probably better ways of doing it.

addy_010 said:
why is it bad pratice to put <? and <?php? do they mean differnt things? the reason i have used them is because i was using tutorials to write the php and some would say ok start off with one and others would start with the other

Well from what I understand, the <? bit is known as a short tag and some web hosts can turn it off making your scripts not work so I find it best to use the full <?php bit. There's no difference in what they do I don't think though.

addy_010 said:
also wanting to try out yahoo ads on my site, however i can't seem to find a way to get them. can someone post me a link to set them up for my site. i honestly have searched, and i can't find them, just yahoo financing and some other things

Haven't a clue sorry, never used them :o
 
addy_010 said:
why is it bad pratice to put <? and <?php? do they mean differnt things? the reason i have used them is because i was using tutorials to write the php and some would say ok start off with one and others would start with the other
addy_010 said:
why is it bad pratice to put <? and <?php? do they mean differnt things? the reason i have used them is because i was using tutorials to write the php and some would say ok start off with one and others would start with the other
<? is a shorthand way of writing <?php. It also allows a few other shorthand tricks such as <?= being equivalent to <?php print.

Problem: <? is not always supported in all PHP installations [affected by the short_open_tag ini option], and PHP can be tripped up if you're working with something like XML that uses <?xml. Stick with <?php in all cases, unless you specifically know you need to use the shorthand.

On another note, brackets aren't required after include or require. They are a language construct and not a function, so prefer:
Code:
include 'foobar.php';
over
Code:
include('foobar.php');
also wanting to try out yahoo ads on my site, however i can't seem to find a way to get them. can someone post me a link to set them up for my site. i honestly have searched, and i can't find them, just yahoo financing and some other things
Probably because Y!PN is still in beta, and only available to US residents, with US traffic :) - see links on http://publisher.yahoo.com/.
 
ok thanks guys for the help i think thats pretty much everything. One last thing actually is that is there a way to make something begin at the root directory and then work it's way to the directory specified? for instance the form i have been talking about is on submit.php but that is in root/submit/ folder. i have the login script in root/login/admin/ etc. Is there a way that i can just put like .../root/etc that will just begin at the first directory, instead of ../ which just goes back one?
 
cheers bud

[edit]
seeing as my thread is at the top i am not gonig to bother making a new thread for this.

wheni started the site i was uploading example php stuff, such as counters. All of which have then been modified, deleted, etc. Im having a problem though as one of the scruipts put a folder up with a file in it which i can't now delete. I have tried many things such as renemaing it, not allowed, changing its permission, not allowed. So im asking is there another way in which i can try delete it, or if all fails, dleelte everything on the server and then just upload it all again as i jhave it all still here.

thanks
 
Last edited:
I've had a similar problem in the past, I believe it was because the script had created several files/directories, and I didn't have ownership of them to be able to delete them. I think the solution I used was to use another script to delete the files. Although your host should be able to do it for you aswell.
 
Back
Top Bottom