Take the 2-minute tour ×

When I am trying to execute following code to email the contact form details, it is not executing properly. Instead, when the contact form's Submit button is clicked, it just shows the below source code in the browser. What's wrong?


    $firstname = $_POST['fname'];
    $emailaddress = $_POST['eaddress'];
    $mobile = $_POST['cellno'];
    $phone = $_POST['landline'];
    $country = $_POST['ucountry'];
    $city = $_POST['ucity'];
    $subjects = $_POST['usubjects'];
    $message = $_POST['umessage'];

    $email_to = "abc@example.com";
    $email_subject = $subjects;

function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form your submitted. ";

    // validation expected data exists
    if(!isset($firstname) ||
        !isset($emailaddress) ||
        !isset($subjects) ||
        !isset($message)) {
        died('We are sorry, but there appears to be a problem with the form your submitted.');      

    $error_message = "";
  if(!eregi($email_exp,$emailaddress)) {
    $string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$firstname)) {

  if(strlen($message) < 2) {
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$phone)) {
    $error_message .= 'The Telphone Number you entered does not appear to be valid.<br />';
  if(strlen($error_message) > 0) {

    function clean_string($string) {

    $email_message .= "First Name: ".clean_string($firstname)."\n";
    $email_message .= "Last Name: ".clean_string($mobile)."\n";
    $email_message .= "Email: ".clean_string($emailaddress)."\n";
    $email_message .= "Telephone: ".clean_string($phone)."\n";
    $email_message .= "City: ".clean_string($city)."\n";
    $email_message .= "Telephone: ".clean_string($country)."\n";
    $email_message .= "Comments: ".clean_string($message)."\n";

    // create email headers
share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

If the source code is shown, this means that the server does not recognize this file as PHP code. The reasons may be different. First of all (most probable) the extension of the file is unrecognized. Second option - the server does not allow PHP

share|improve this answer
The extension is .PHP. I am testing the file locally and it is not on any server. –  RKh Mar 18 '10 at 13:33
@RPK if it's not on a server, therein might lie your problem :-) –  richsage Mar 18 '10 at 13:36
Exactly. Do you have any kind of server installed on your machine then? Like Apache or IIS? How you enter the URL? using localhost/xxxx or just opening file with page in a browser? –  Tomasz Struczyński Mar 18 '10 at 13:38
I just uploaded the files on my remote server. Now it is giving error: 500 - Internal server error. There is a problem with the resource you are looking for, and it cannot be displayed. –  RKh Mar 18 '10 at 15:10
Hooray. Then turn on logging on server and check the logs to see what exactly is wrong. error_reporting(E_ALL); ini_set('display_errors', true); –  Tomasz Struczyński Mar 19 '10 at 13:26
add comment

If your seeing the source code in your browser, it's likely that Apache isn't processing the file, but is serving it as plain text to the browser. Your file should end in .php and your Apache server must be configured to use mod_php to process .php files.

Start here: http://dan.drydog.com/apache2php.html


If you are indeed simply testing these files on your local machine, PHP cannot work. It is a server-side language; that is, it requires a server to interpret the PHP parts of the file and send the output to your browser. You'll either need to install XAMPP, WAMP or find a 3rd party hosting service that will allow you to upload your PHP files.


We cannot fix your script with more information about the error. Begin reducing the complexity of the script in large steps until the error is resolved, then begin reintroducing functionality in small steps until you encounter the error again. Then you'll know what is wrong, and hopefully how to fix it.

Once thing I notice is the function error_notice isn't defined, nor is it built-in to PHP. Begin by commenting out that line.

share|improve this answer
Please see my comment to Tomasz above. –  RKh Mar 18 '10 at 15:11
add comment

1) make sure that the php module is compiled and running on your apache.

LoadModule php5_module /usr/lib/apache2-prefork/mod_php5.so

run apachectl -t -D DUMP_MODULES : show all loaded modules to view all loaded modules.

2) make sure the handlers are set on your apache AddHandler application/x-httpd-php .php4 AddHandler application/x-httpd-php .php5 AddHandler application/x-httpd-php .php

3) test by running a simple script on the server. create php file phpinfo.php then ass test if the script runs, if it does not, then double check the apache configuration again...

share|improve this answer
add comment

Your Answer


