$PHP_SELF and include files


I am trying to create a contact form that sends a e-mail to a specified e-mail account. Thus far I have been able to send an e-mail through the form. For security reasons and also to filter out rubbish, the data needs to be validated before sending the e-mail.

After a user submits the a form the data needs to be validated and if filled out correctly an e-mail is send. If not the user needs to stay on the same page with a warning that the data is missing or not correct.

To accomplish this I want to use the $PHP_SELF action. However, for the layout my site uses templates that are included through an include_once function. At first I want to use an IF / ElSE statement in combination with to inject the template with HEREDOC. However, with HEREDOC I haven’t been able to use my include files.

Could someone offer me general advice how to accomplish this?


Sorry, I must have been using the wrong keywords for my search. Where can I find an earlier discussion on this topic?

I am getting the basics, but more information would be helpful.


//php part

do form processing

//on success

//you need footer you can include it here…
//not clean method …many idea here



show form in plain html
in value do
value=<?php if (isset($_POST[‘value’])) echo $_POST[‘value’]; ?>

just a baisc algorithm
hope this helps…

There are pretty much discussions on this before in this forum. So please try to search in the forum before you discuss more here. But what I would recommend is:

  • Make a form that is submitted in the page.
  • Check whether the form is posted in the page on top of the page.
    (very top of the page so that you can redirect the browser upon success to another page without header already sent warning)
  • Check if any errors found. If not found then do send email. Otherwise let the process on the same page.

See the following script:

    $error = array();
    $name = mysql_real_escape_string($_POST['name']);
        $error['name'] = 'Name is required';
        // do send emails or do whatever you want here
        // redirect to thank you page or some other pages or at least redirect the browser to the same page.
        // so that it wont resubmit the form when refreshed the page.
    // else it will automatically stay in the same page
// your form will be here.

Please read the comments carefully.