little help for a php noob

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
johnnyblotter
Forum Newbie
Posts: 2
Joined: Fri Jul 09, 2010 1:26 pm

little help for a php noob

Post by johnnyblotter »

Hi folks.

I am doing a very simple php require statement to include the header on every page of the site.

Code: Select all

<?php require("includes/main-navigation.php"); ?>
The problem is with a form validation script. The validation script originally was set to embed either "error.htm" or "success.htm" into the page. Since I want the error page and success pages to display like other pages, I added the require statement to the these pages, and saved them as error.php and success.php. What happens is the page that's served won't output the php, and just shows the php statement in the source code.

The form validation is long but here's the relevant bit:

Code: Select all

// Embed error page and dump it to the browser

$fileErrorPage = 'error.php';

if ( $validationFailed == true) {
    if ( file_exists( $fileErrorPage) === false) {
        echo 'The error page: <b>' . $fileErrorPage . '</b> cannot be found on the server.';
        exit;
    }

    $fileHandle = fopen ( $fileErrorPage, 'r');
    $errorPage = fread ( $fileHandle, filesize( $fileErrorPage));
    fclose ( $fileHandle);

    $errorPage = str_replace( '<!--VALIDATIONERROR-->', $errorList, $errorPage);
    echo $errorPage;
    exit;
}

// END ERROR VALIDATION SECTION

$fileSuccessPage = 'success.php';

if ( file_exists( $fileSuccessPage) === false) {
    echo 'The success page: <b> ' . $fileSuccessPage . '</b> cannot be found on the server.';
    exit;
}

$fileHandle = fopen ( $fileSuccessPage, "r");
$successPage = fread ( $fileHandle, filesize( $fileSuccessPage));
fclose ( $fileHandle);

echo $successPage;

exit;

Any idea why the page that's served wouldn't be able to output the php? Thanks....
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: little help for a php noob

Post by Jade »

Why are you reading in the page with fopen? Either include it or redirect to it.

Code: Select all

// Embed error page and dump it to the browser

$fileErrorPage = 'error.php';

if ( $validationFailed == true) {
    if ( file_exists( $fileErrorPage) === false) {
        echo 'The error page: <b>' . $fileErrorPage . '</b> cannot be found on the server.';
        exit;
    }

    //redirect to the error page
    header("Location: " . $fileErrorPage);

     //or include the error page
    include($fileErrorPage);
    exit;
}
johnnyblotter
Forum Newbie
Posts: 2
Joined: Fri Jul 09, 2010 1:26 pm

Re: little help for a php noob

Post by johnnyblotter »

hmmm. That allows the correct file to display and output the php, but I lose all my validation errors...

Code: Select all

$errorPage = str_replace( '<!--VALIDATIONERROR-->', $errorList,  $errorPage);
    echo $errorPage;
    exit;
On the error.php page, <!--VALIDATIONERROR--> is not replaced with the errors as it should be....

I know I'm a noob! Can anyone help me sort this out? Thanks...
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: little help for a php noob

Post by Jade »

Change your error page so that it uses <?php echo $VALIDATORERROR; ?> and then you can set $VALIDATORERROR prior to including the $errorPage. That way when the error page is displayed the $VALIDATORERROR is within the scope and it'll display whatever you want it to.
Post Reply