Jump to content

Recommended Posts

Hi All,

 

The following code is an include on my site. What I'm after is a redirect, using headers is the best & quickest method, however, whenever I try this, I get the damned headers sent message. Is this because it's an include file? here's my code:

<?php require_once "/lib/viewer_functions.php";

  // process form
  if (@$_REQUEST['submitForm']) {

    // error checking
    $errorsAndAlerts = "";
$check = $_REQUEST['check'];
$randomNumber = $_REQUEST['randomNumber'];
    if (!@$_REQUEST['name'])                	{ $errorsAndAlerts = "Please add your name\n"; }
    if (!@$_REQUEST['email'])                   { $errorsAndAlerts = "Please add your email\n"; }
    else if(!isValidEmail(@$_REQUEST['email'])) { $errorsAndAlerts = "That email address is not valid\n"; }
    if (!@$_REQUEST['enquiry'])                 { $errorsAndAlerts = "Please add your enquiry\n"; }
if (!@$_REQUEST['randomNumber'])          	{ $errorsAndAlerts = "Please verify the number\n"; }

$textToConvert = $_REQUEST['enquiry'];
$convertedText = iconv("UTF-8", "ISO-8859-1", $textToConvert);

    // send email user
    if ((!$errorsAndAlerts) && ($randomNumber == $check)) {
      $to      = "info@domain.com";	
      $subject = "Quick Contact";
  $email    = "info@domain.com";

  $headers  = "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
  $headers .= "From: $email \r\n";
  
  $body =  "<strong>Full name</strong>: ". $_REQUEST['name'] ."<br />";
  $body .= "<strong>Email</strong>: ".$_REQUEST['email'] ."<br />";
  $body .= "<strong>Enquiry</strong>: $convertedText<br /><br />";
  $body .= "The user who sent this message had the IP address <strong>".$_SERVER['REMOTE_ADDR']."</strong><br />";

  $message = $body;	  

      // send message
      $mailResult = @mail($to, $subject, $message, $headers);
      if (!$mailResult) { die("Mail Error: $php_errormsg"); }

      //
      //$errorsAndAlerts = "Thanks!  We've sent your email.";
header("Location: /thanks.php"); exit();
      $_REQUEST = array(); // clear form values

}

  }

?>
<?php $randomNumber = rand(1,10); ?> 
<!-- right quick_contact form -->

<!-- quick_contact_title -->
<div class="rh_quick_contact_title">
	<img src="images/rh_quick_enquiry_title.jpg" width="232" height="31" />
</div>
<!-- /quick_contact_title -->		


<!-- quick_contact_form -->
<div class="rh_quick_contact_content_larger">
<form method="post">
	<input type="hidden" name="submitForm" value="1" />

	<?php if (@$errorsAndAlerts): ?>
		<div style="color: red; font-weight: bold; font-size: 12px; font-family: arial;">
		  <?php echo $errorsAndAlerts; ?>
		</div>
	 <?php endif ?>

	<table width="213" border="0" cellpadding="2" cellspacing="2">
		<tr>
			<td align="center">
				<input name="name" type="text" id="name" style="width:180px;" value="Name..." onclick="clickclear(this, 'Name...')" onblur="clickrecall(this,'Name...')" />
			</td>
		</tr>
		<tr>
			<td align="center">
				<input name="email" type="text" id="email" style="width:180px;" value="Email Address..." onclick="clickclear(this, 'Email Address...')" onblur="clickrecall(this,'Email Address...')"/>
			</td>
		</tr>
		<tr>
			<td align="center">
				<textarea name="enquiry" id="enquiry" value="Enquiry..." onclick="clickclear(this, 'Enquiry...')" onblur="clickrecall(this,'Enquiry...')" style="width:180px;" rows="3"></textarea>
			</td>
		</tr>	
		<tr>
			<td align="center"><input type="text" id="randomNumber" name="randomNumber" style="width:20px;" /> Please enter the following number in the left hand box: <?php echo $randomNumber; ?>
	</td>
		</tr>				
		<tr>
			<td><div align="right">
				<input type="image" src="images/blue_submit_button.jpg" value="Submit" alt="Submit">  
			</div></td>
		</tr>										
	</table>	
<input type="hidden" id="check" name="check" value="<?php echo $randomNumber;?>" />
</form>
</div>
<!-- /quick_contact_form -->

 

As always, any help is much appreciated!

Thanks

Link to comment
https://forums.phpfreaks.com/topic/195560-headers-sent-issue-on-include-file/
Share on other sites

Thanks for the replies, the error says:

 

Warning: Cannot modify header information - headers already sent by (output started at \inc\header.php:139) in \inc\quick_enquiry_form2.php on line 44

 

Line 139 in header.php is the navigation and line 44 of quick_enquiry_form2.php has no spacing and looks like

[php      $mailResult = @mail($to, $subject, $message, $headers);

      if (!$mailResult) { die("Mail Error: $php_errormsg"); }

  header("Location:thanks.php");[/code]

There are also no spaces at the top of that file.

 

Sorry, someone is gonna have to speak & spell it to me! :-(

Why are you (attempting) to output your page contents up to the point of the header() statement, then redirect to a different page? If you could do this, it would waste your hosting bandwidth on every page request and waste the processing time it takes to generate and output that content, so you are fortunate that it does not work, your site would operate poorly.

 

Why not just output the thank you content on the page you are already on? A header() redirect tells the browser to request the target page, the same as if you browsed to it. That in itself takes extra time.

Hi PFMaBiSmAd,

Because of the way the site was constructed, I thought this would be the best way. The structure is:

Header

Page name, for example, index, about, thanks etc (which has several includes 1 being the enquiry form)

Footer

 

The enquiry form says thank you once you submit it, however, it would be better to fill in the form and send them to the thanks page. I thought header() was the only option to do this. If there is another way, I'm all ears! However, as this is a live site, I don't want to recode pages

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.