homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP - Need to output a global 'include()' from a function.
include() is in the function scope, it need to be global

10+ Year Member

Msg#: 389 posted 10:07 pm on Feb 13, 2002 (gmt 0)

Hi. I have a page with forms on it. When an error occurs. I want the script to diplay whats wrong (like a missing field), then inlcude the form with all the data they've just entered, then stop the script from entering it in to the database. This is what I made:

function error($error, $include) {

if (!empty($include)) {
echo '<SPAN CLASS="error">ERROR:</SPAN>'.$error.'.<P></DIV></DIV><DIV ID="mainContent">';
echo '</DIV></BODY></HTML>';

} else {
echo '<SPAN CLASS="error">ERROR:</SPAN>'.$error.'.<P>';

This function seemed ok in theroy--I'd call error('blah is blank', 'form.inc') at an error. Untill I realised that it wasn't working because the 'include()' will try to include it in to the function. And it won't be global, which means that all the fileds will be blank, and the page will bascily screw up.

Is there anyway I can 'output' this into the main script? If I used an include(), then I'd use the abilioty to pass varibles through to it.

Any ideas?




WebmasterWorld Senior Member 10+ Year Member

Msg#: 389 posted 10:33 am on Feb 15, 2002 (gmt 0)

> If I used an include(), then I'd use the abilioty to pass varibles through to it.

Justin, I'm not sure I understand your question. The script you call with the include() function will inherit all the variables previously set - you don't need to explicitly pass variables to it.


10+ Year Member

Msg#: 389 posted 7:48 pm on Feb 15, 2002 (gmt 0)

It might supposed to. But it doen't. I can't remember reading anywhere that includes are an exception to the scope rule of functions.

When I meant that I'd lose the ablity to pass varibles though it, i mean things like an error message. At every error. I'd have to have:

echo '<SPAN CLASS="error">Error: error message</SPAN></DIV></DIV><DIV ID="main">'; include('blah.inc'); echo '</DIV></BODY></HTML>';

That's a bit silly considering how many fields I need to have an error message for. A better way I though would be to have:

error('error message', 'blah.inc');

But as I explained before, that won't work. Because in the incude it's self, is being called inside a function, so none of the varibles I need for the include will be there. I could pass them though. But that wold be a huge list, and would be a bit silly. I though there must be away to get the include called from inside a function, be included as if it had been called outside the function.


10+ Year Member

Msg#: 389 posted 8:40 pm on Feb 15, 2002 (gmt 0)

Hi Justin,
I tried doing something similar to what your doing. What I got is that as soon as you declare the include function. PHP will automatically include the file specified. It does not matter if it's in a variable at all.

Why not check the form in the same file?

Like so

$error = "<font face=\"Arial\" size=\"2\" color=\"red\"><b>You need to fill out the Name field before submitting</b></font><br>";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


<form action="" method="post">
if ($submit == true) {
if (empty($name)) {
print $error;
else {
print "Do Database stuff<br>";
Enter Name:<br>
<input type="Text" name="name">
<input type="Submit" name="submit" value="Submit">


10+ Year Member

Msg#: 389 posted 10:39 pm on Feb 15, 2002 (gmt 0)

It's no the fact that it's a varible that's stuffing things up. It's that fact that the script being included, is only using varibles within the scope of the function that it was called in.

I already have it on the same page. But the script is complex. And it has over 40 fileds, and calls about 10 differect insert or update querys. Once an error occurs. I have to exit; before it tries to insert data into other records.
The easiest way I thought would be to have a function which stops the script, then includes the from again, so the data is not lost. And gets rid of the cache expired thing if they hit the back button, with might cause them to re-load, creating a duplicate entry.
The onlyt other way I can think of is to have a complex set of nested 'if's and 'else's. My brain isn't up to that.

Acctualy. Now that I think about it. I can probably do a bit of rearranging of the script and do all the validation before any database stuff. An and use a system like

function error($error, $x) {
global $ok;
echo 'blah'.$error.'blah';
if ($x == 'n') { $ok = 'n'; }

if (bad happens) { error('blah', 'n'); )

if ($ok = 'y') { database stuff }

Although. It still would be hand to know the answer to my main question, incase I ever need to do it agian.

As you might have guessed. This is my first big PHP project. There is probably a better way of setting the page up in the first place.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved