Redirect in called function not working

I need help redirecting to a new web page from a called PHP function. My situation:

I have a short input form on an SHTML page with a “submit” button. Clicking the submit button calls PHP function “SendVideoRequest” that edits the form data and builds and sends the data to me in an email. The code is working just fine (pertinent code from calling page below):

[code]

Page Title [/code]

Currently, after sending the email, the PHP function displays an alert to the effect “Form data has been submitted.” I want to change this so that instead of sending the alert, control is transferred to a new SHTML page. I’ve tried the “header/Location” command as shown below, but it throws an error. I’ve tried putting in “ob_start() - ob_end_flush” in the function also, to no avail. Why won’t “header/Location” work in this scenario? Any suggestions on a different approach?

Function “SendVideoRequest” code:

[php]<?php
$toName = ‘’;
$subject = $_REQUEST[‘emailSubject’];
$fromName = $_REQUEST[‘custName’];
$fromAddress = $_REQUEST[‘emailAddressFrom’];
$videoID = $_REQUEST[‘videoID’];
$preroll = $_REQUEST[‘preroll’];
$postroll = $_REQUEST[‘postroll’];
$toAddress = ‘[email protected]’;
$bodyText = stripslashes($_REQUEST[‘emailBody’]);
$bodyTextHTML = $bodyText;
$bodyHTML;
$linkTargets = array();
$linkTargets[] = ‘(http://www.myurl.com)’;
$linkTargets[] = ‘(http://myurl.com)’;
$linkTargets[] = ‘(www.myurl.com)’;
$linkTargets[] = ‘(myurl.com)’;
$linkPre = ’ ';
$linkPost = ‘
’;
$tLinkString = “”;
$tLinkTarget = “”;

$bodyTextHTML = ereg_replace(implode('|', $linkTargets), $linkPre . 'http://www.myurl.com' . $linkPost, $bodyTextHTML);
$bodyTextHTML = str_replace("\n", '<br />' . "\n", $bodyTextHTML);

if (strlen($fromName) && strlen($fromAddress) && strlen($videoID) && strlen($preroll) && strlen($postroll)) {
	$bodyHTML = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type"><link rel="stylesheet" type="text/css" href="http://www.myurl.com/pwstylemail.css"></head><body bgcolor="#ffffff" text="#000000"><div class="emailContentDIV">' . $bodyTextHTML . '</div></body></html>';
	SendEmail($toName, $toAddress, $fromName, $fromAddress, $subject, $bodyText, $bodyHTML);
	header('Location: http://www.myurl.com/testit.shtml');
	exit();
} else {
	WriteCommand("alert('All fields must contain valid data.');");
}

?>[/php]

Is SendEmail() echoing anything to the browser?

Nope, “SendEmail” code is as follows:

[php]<?php

function SendEmail($toName, $toAddress, $fromName, $fromAddress, $subject, $bodyText, $bodyHTML) {
$toField = ‘’;
$fromField = ‘’;

if (strlen($toName) && strlen($toAddress)) {
	$toField = $toName . " <" . $toAddress . ">";
} elseif ($toAddress) {
	$toField = $toAddress;
}# end if/else

if (strlen($fromName) && strlen($fromAddress)) {
	$fromField = $fromName . " <" . $fromAddress . ">";
} elseif ($fromAddress) {
	$fromField = $fromAddress;
}# end if/else

if (strlen($toField) && strlen($fromField) && strlen($subject) && strlen($bodyText) && strlen($bodyHTML)) {
	$boundary = "====" . time() . "====";

	$headers = <<<END_OF_HTML

From: $fromField
Reply-To: $fromField
Subject: $subject
MIME-Version: 1.0
Content-type: multipart/alternative; boundary="$boundary"

This is a multi-part message in MIME format.

–$boundary
Content-type: text/plain; charset=“iso-8859-1”

$bodyText

–$boundary
Content-type: text/html; charset=“iso-8859-1”

$bodyHTML

–$boundary–
END_OF_HTML;
mail($toField, $subject, ‘’, $headers);
}# end if

}# end SendEmail
?>[/php]

What is the error? I’m guessing it’s “output already sent”

In the browser, it just displays “Error on page” and remains on the calling page.

Well it’s not a PHP error. You need to figure out what is calling that error. If any output (errors, even a blank line above your php tag) is sent to the browser the header() will not work.

Well, I turned on browser debugging and it appears to be choking on a JScript function in a “commonservices” file. Specifically, a “syntax error” on the following line of code:

eval(xhr.responseText);

in this JScript function:

// Handles ReadyStateChange function processReadyStateChange(){ // check readyState for 'loaded' status if(xhr.readyState == 4){ //check status for 'OK' if(xhr.status == 200){ if(xhr.responseText != ""){ eval(xhr.responseText); responseTime = Math.abs((new Date()).getUTCMilliseconds() - responseTimeStart); if(showResponseTime){ alert("Response Time: " + responseTime + "ms"); }// end if initXhr(); }// end if } else{ alert("An error occurred while retrieving the XML data: " + xhr.statusText); }// end if/else }// end if readyState is 'loaded' }// end processReadyStateChange

I don’t know what this implies, so I guess it may be time to try some javascript to do the redirect.

Sponsor our Newsletter | Privacy Policy | Terms of Service