$PHP_SELF and include files

Hi,

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?

Hi,

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.

header

<?php
//php part

if(isset($_POST[‘submit’])
{
do form processing

//on success
require_once(‘footer.php’);
exit();

//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…
thanks
footer.php

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:


if($_SERVER['REQEUST_METHOD'] == 'POST'){
    $error = array();
    $name = mysql_real_escape_string($_POST['name']);
    if(empty($name)){
        $error['name'] = 'Name is required';
    }
    if(empty($error)){
        // 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.