0

Hello,

I know this question was answered many times, and I looked at the answers but still can't find a solution.

I have an index file that includes a contact form.

The contact form action is itself, and if the form succeeds it should redirect to index.php?content=thankyou

Althought the form is being sent successfuly, it does not redirect to index.php?content=thankyou, it tries to go again to itself: index.php?content=contact and I get the "cannot modify header..." error.

I've added ob_start, ob_flush, I checked for spaces after php codes, or anywhere else, nothing helps.

I am attaching the index, contact and the form class files.

The problem might be in the index file line: 56, or on the fgcontactform file line: 143.

I am losing hair here, please, can someone help?

Attachments
<!-- Form Code Start -->
<div class="inner" style="padding-top: 40px;">
<div style="width: 345px; margin: 0 auto;">
<form id='contactus' action='<?php echo $formproc->GetSelfScript(); ?>?content=contact' method='post' accept-charset='UTF-8'>
<fieldset >
<legend>Sigal Zahavi - Contact</legend>

<input type='hidden' name='submitted' id='submitted' value='1'/>
<input type='hidden' name='<?php echo $formproc->GetFormIDInputName(); ?>' value='<?php echo $formproc->GetFormIDInputValue(); ?>'/>
<input type='text'  class='spmhidip' name='<?php echo $formproc->GetSpamTrapInputName(); ?>' />
<h3>083 289 5280</h3>
<div class='short_explanation'>* Required fields</div>

<div><span class='error'><?php echo $formproc->GetErrorMessage(); ?></span></div>
<div class='container'>
    <label for='name' >Name:* </label><br/>
    <input type='text' name='name' id='name' value='<?php echo $formproc->SafeDisplay('name') ?>' maxlength="50" /><br/>
    <span id='contactus_name_errorloc' class='error'></span>
</div>
<div class='container'>
    <label for='email' >Email:*</label><br/>
    <input type='text' name='email' id='email' value='<?php echo $formproc->SafeDisplay('email') ?>' maxlength="50" /><br/>
    <span id='contactus_email_errorloc' class='error'></span>
</div>

<div class='container'>
    <label for='email' >Phone:</label><br/>
    <input type='text' name='phone' id='phone' value='<?php echo $formproc->SafeDisplay('phone') ?>' maxlength="10" />
</div>
<div class='container'>
    <label for='message' >Message:*</label><br/>
    <span id='contactus_message_errorloc' class='error'></span>
    <textarea rows="10" cols="50" name='message' id='message'><?php echo $formproc->SafeDisplay('message') ?></textarea>
</div>
<div class='container'>
    <input type='submit' name='Submit' value='Send' />
</div>
</fieldset>
</form>
</div>
</div>
<!-- client-side Form Validations:
Uses the excellent form validation script from JavaScript-coder.com-->
<script type='text/javascript'>
// <![CDATA[
    var frmvalidator  = new Validator("contactus");
    frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();
    frmvalidator.addValidation("name","req","Please enter your name");

    frmvalidator.addValidation("email","req","Please enter your email address");

    frmvalidator.addValidation("email","email","The email address you have entered is not valid");

		frmvalidator.addValidation("message","req","Please write a message");
    frmvalidator.addValidation("message","maxlen=2048","Your message is over the size limit!(More than 2KB!)");
// ]]>
</script>
<?PHP
/*
    Contact Form from HTML Form Guide

    This program is free software published under the
    terms of the GNU Lesser General Public License.

This program is distributed in the hope that it will
be useful - WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

@copyright html-form-guide.com 2010
*/
require_once("class.phpmailer.php");

/*
Interface to Captcha handler
*/
class FG_CaptchaHandler
{
    function Validate() { return false;}
    function GetError(){ return '';}
}
/*
FGContactForm is a general purpose contact form class
It supports Captcha, HTML Emails, sending emails
conditionally, File atachments and more.
*/
class FGContactForm
{
    var $receipients;
    var $errors;
    var $error_message;
    var $name;
    var $email;
		var $phone;
    var $message;
    var $from_address;
    var $form_random_key;
    var $conditional_field;
    var $arr_conditional_receipients;
    var $fileupload_fields;
    var $captcha_handler;

    var $mailer;

    function FGContactForm()
    {
        $this->receipients = array();
        $this->errors = array();
        $this->form_random_key = 'HTgsjhartag';
        $this->conditional_field='';
        $this->arr_conditional_receipients=array();
        $this->fileupload_fields=array();

        $this->mailer = new PHPMailer();
        $this->mailer->CharSet = 'utf-8';
    }

    function EnableCaptcha($captcha_handler)
    {
        $this->captcha_handler = $captcha_handler;
        session_start();
    }

    function AddRecipient($email,$name="")
    {
        $this->mailer->AddAddress($email,$name);
    }

    function SetFromAddress($from)
    {
        $this->from_address = $from;
    }
    function SetFormRandomKey($key)
    {
        $this->form_random_key = $key;
    }
    function GetSpamTrapInputName()
    {
        return 'sp'.md5('KHGdnbvsgst'.$this->GetKey());
    }
    function SafeDisplay($value_name)
    {
        if(empty($_POST[$value_name]))
        {
            return'';
        }
        return htmlentities($_POST[$value_name]);
    }
    function GetFormIDInputName()
    {
        $rand = md5('TygshRt'.$this->GetKey());

        $rand = substr($rand,0,20);
        return 'id'.$rand;
    }

    function GetFormIDInputValue()
    {
        return md5('jhgahTsajhg'.$this->GetKey());
    }

    function SetConditionalField($field)
    {
        $this->conditional_field = $field;
    }
    function AddConditionalReceipent($value,$email)
    {
        $this->arr_conditional_receipients[$value] =  $email;
    }

    function AddFileUploadField($file_field_name,$accepted_types,$max_size)
    {

        $this->fileupload_fields[] =
            array("name"=>$file_field_name,
            "file_types"=>$accepted_types,
            "maxsize"=>$max_size);
    }

    function ProcessForm()
    {
        if(!isset($_POST['submitted']))
        {
           return false;
        }
        if(!$this->Validate())
        {
            $this->error_message = implode('<br/>',$this->errors);
            return false;
        }
        $this->CollectData();

        $ret = $this->SendFormSubmission();

        return $ret;
    }

    function RedirectToURL($url)
    {
       header("Location: $url");
       exit;
    }

    function GetErrorMessage()
    {
       return $this->error_message;
    }
    function GetSelfScript()
    {
       return htmlentities($_SERVER['PHP_SELF']);
    }

    function GetName()
    {
        return $this->name;
    }
    function GetEmail()
    {
        return $this->email;
    }
		function GetPhone()
		{
			return $this->phone;
		}
    function GetMessage()
    {
        return htmlentities($this->message,ENT_QUOTES,"UTF-8");
    }

/*--------  Private (Internal) Functions -------- */


    function SendFormSubmission()
    {
        $this->CollectConditionalReceipients();

        $this->mailer->CharSet = 'utf-8';
        
        $this->mailer->Subject = "Contact form submission from sigalz.co.za by $this->name";

        $this->mailer->From = $this->GetFromAddress();

        $this->mailer->FromName = $this->name;

        $this->mailer->AddReplyTo($this->email);

        $message = $this->ComposeFormtoEmail();

        $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
        $this->mailer->AltBody = @html_entity_decode($textMsg,ENT_QUOTES,"UTF-8");
        $this->mailer->MsgHTML($message);

        $this->AttachFiles();

        if(!$this->mailer->Send())
        {
            $this->add_error("Failed sending email!");
            return false;
        }

        return true;
    }

    function CollectConditionalReceipients()
    {
        if(count($this->arr_conditional_receipients)>0 &&
          !empty($this->conditional_field) &&
          !empty($_POST[$this->conditional_field]))
        {
            foreach($this->arr_conditional_receipients as $condn => $rec)
            {
                if(strcasecmp($condn,$_POST[$this->conditional_field])==0 &&
                !empty($rec))
                {
                    $this->AddRecipient($rec);
                }
            }
        }
    }

    /*
    Internal variables, that you donot want to appear in the email
    Add those variables in this array.
    */
    function IsInternalVariable($varname)
    {
        $arr_interanl_vars = array('scaptcha',
                            'submitted', 'Submit',
                            $this->GetSpamTrapInputName(),
                            $this->GetFormIDInputName()
                            );
        if(in_array($varname,$arr_interanl_vars))
        {
            return true;
        }
        return false;
    }

    function FormSubmissionToMail()
    {
        $ret_str='';
        foreach($_POST as $key=>$value)
        {
            if(!$this->IsInternalVariable($key))
            {
                $value = htmlentities($value,ENT_QUOTES,"UTF-8");
                $value = nl2br($value);
                $key = ucfirst($key);
                $ret_str .= "<div class='label'>$key :</div><div class='value'>$value </div>\n";
            }
        }
        foreach($this->fileupload_fields as $upload_field)
        {
            $field_name = $upload_field["name"];
            if(!$this->IsFileUploaded($field_name))
            {
                continue;
            }        
            
            $filename = basename($_FILES[$field_name]['name']);

            $ret_str .= "<div class='label'>File upload '$field_name' :</div><div class='value'>$filename </div>\n";
        }
        return $ret_str;
    }

    function ExtraInfoToMail()
    {
        $ret_str='';

        $ip = $_SERVER['REMOTE_ADDR'];
        $ret_str = "<div class='label'>IP address of the submitter:</div><div class='value'>$ip</div>\n";

        return $ret_str;
    }

    function GetMailStyle()
    {
        $retstr = "\n<style>".
        "body,.label,.value { font-family:Arial,Verdana; direction: rtl;} ".
        ".label {font-weight:bold; margin-top:5px; font-size:1em; color:#333;} ".
        ".value {margin-bottom:15px;font-size:0.8em;padding-left:5px;} ".
        "</style>\n";

        return $retstr;
    }
    function GetHTMLHeaderPart()
    {
         $retstr = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n".
                   '<html><head><title></title>'.
                   '<meta http-equiv=Content-Type content="text/html; charset=utf-8">';
         $retstr .= $this->GetMailStyle();
         $retstr .= '</head><body>';
         return $retstr;
    }
    function GetHTMLFooterPart()
    {
        $retstr ='</body></html>';
        return $retstr ;
    }
    function ComposeFormtoEmail()
    {
        $header = $this->GetHTMLHeaderPart();
        $formsubmission = $this->FormSubmissionToMail();
        $extra_info = $this->ExtraInfoToMail();
        $footer = $this->GetHTMLFooterPart();

        $message = $header."Information request from sigalz.co.za<p>$formsubmission</p><hr/>$extra_info".$footer;

        return $message;
    }

    function AttachFiles()
    {
        foreach($this->fileupload_fields as $upld_field)
        {
            $field_name = $upld_field["name"];
            if(!$this->IsFileUploaded($field_name))
            {
                continue;
            }
            
            $filename =basename($_FILES[$field_name]['name']);

            $this->mailer->AddAttachment($_FILES[$field_name]["tmp_name"],$filename);
        }
    }

    function GetFromAddress()
    {
        if(!empty($this->from_address))
        {
            return $this->from_address;
        }

        $host = $_SERVER['SERVER_NAME'];

        $from ="nobody@$host";
        return $from;
    }

    function Validate()
    {
        $ret = true;
        //security validations
        if(empty($_POST[$this->GetFormIDInputName()]) ||
          $_POST[$this->GetFormIDInputName()] != $this->GetFormIDInputValue() )
        {
            //The proper error is not given intentionally
            $this->add_error("Automated submission prevention: case 1 failed");
            $ret = false;
        }

        //This is a hidden input field. Humans won't fill this field.
        if(!empty($_POST[$this->GetSpamTrapInputName()]) )
        {
            //The proper error is not given intentionally
            $this->add_error("Automated submission prevention: case 2 failed");
            $ret = false;
        }

        //name validations
        if(empty($_POST['name']))
        {
            $this->add_error("Please enter your name");
            $ret = false;
        }
        /*else
        if(strlen($_POST['name'])>60)
        {
            $this->add_error("Name is too big!");
            $ret = false;
        }*/

        //email validations
        if(empty($_POST
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
ob_start();
if (!isset($_REQUEST['content']))
{
	$page = "main";
	$style = "main";
}
else
{
	$page = $_REQUEST['content'];
	$style = "submain";
}
			
switch ($page)
{
	case "work":
		$title = "Work Examples";
	  break;
	case "education":
		$title = "Education";
		break;
	case "employment":
		$title = "Recent Employment";
		break;
	case "contact":
		$title = "Contact Me";
		break;
	case "thankyou":
		$title = "Thank you";
		break;
	default:
	  $title = "Home";
}
$nextPage = "content/$page.inc.php";

if($page == 'contact')
{
	require_once("content/fgcontactform.php");

	$formproc = new FGContactForm();

	//1. Add your email address here.
	//You can add more than one receipients.
	$formproc->AddRecipient('webmaster@sigalz.co.za'); //<<---Put your email address here

	//2. For better security. Get a random tring from this link: http://tinyurl.com/randstr
	// and put it here
	$formproc->SetFormRandomKey('NDFlEByCt2TkuxL');

	if(isset($_POST['submitted']))
	{
  	if($formproc->ProcessForm())
	  	$formproc->RedirectToURL("index.php?content=thankyou");
	}
	ob_flush();
	
	echo '<link rel="STYLESHEET" type="text/css" href="css/contact.css" />
				<script type="text/javascript" src="scripts/gen_validatorv31.js"></script>';
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Sigal Zahavi - <?php echo $title; ?></title>
<link href="css/mainStyle.css" rel="stylesheet" type="text/css" media="screen"/>
<link href="css/thickbox.css" rel="stylesheet" type="text/css" media="screen" />
<link rel="SHORTCUT ICON" href="http://www.breadandcoffee.co.za/sigalz.co.za/favicon.ico">
<script type="text/javascript" src="scripts/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="scripts/thickbox.js"></script>
<meta name="google-site-verification" content="-ZyDRU7n4zpulcMAoY-qmt-1TFYtubDKuBMTndH62j0" />
</head>
<body>
<?php $offline = FALSE; ?>
<div id="wrapper">
	<div id="header">
		<div id="pageTitle"><?php echo $title; ?></div>
	</div><!-- End header -->
<?php
	if(!$offline) include("content/nav.inc.php");
	echo "<div id=\"" . $style . "\">";
	include($nextPage);
	echo "</div>";
?>
</div><!-- End wrapper -->
</body>
</html>
2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by cgull
0

I noticed this

$formproc->RedirectToURL("index.php?content=thankyou");

and some HTML before it (DOCTYPE, html tags, maybe some more).
That might be the problem.

Edited by Buppy: n/a

0

Hi Buppy,

You are a STAR !!!

I moved the code before the DOCTYPE and now my hair starts growing again :)

THANK YOU !

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.