Can't get form to both go to a different page and send an e-mail

I’ve been using lynda.com tutorials to learn php/mysql in DW 5, along with some help from a friend. I’ve run into a problem I can’t figure out and my friend is in a different time zone, so isn’t available.

The current coding for inserting the record into the db and redirecting the user to the form where they can add a photo if they want is this:

$editFormAction = $_SERVER[‘PHP_SELF’];
if (isset($_SERVER[‘QUERY_STRING’])) {
$editFormAction .= “?” . htmlentities($_SERVER[‘QUERY_STRING’]);
}

if ((isset($_POST[“MM_insert”])) && ($_POST[“MM_insert”] == “form2”)) {
$insertSQL = sprintf(“INSERT INTO people (firstName, lastName, cityName, provName, countryName, phoneNumb, emailAddy, kennelName, breedName, website, Show_Hide, Directory_Listing) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)”,
GetSQLValueString($_POST[‘firstName’], “text”),
GetSQLValueString($_POST[‘lastName’], “text”),
GetSQLValueString($_POST[‘cityName’], “text”),
GetSQLValueString($_POST[‘provName’], “text”),
GetSQLValueString($_POST[‘countryName’], “text”),
GetSQLValueString($_POST[‘phoneNumb’], “text”),
GetSQLValueString($_POST[‘emailAddy’], “text”),
GetSQLValueString($_POST[‘kennelName’], “text”),
GetSQLValueString($_POST[‘breedName’], “text”),
GetSQLValueString($_POST[‘website’], “text”),
GetSQLValueString($_POST[‘Show_Hide’], “text”),
GetSQLValueString($_POST[‘Directory_Listing’], “text”));

mysql_select_db($database_db9568, $db9568);
$Result1 = mysql_query($insertSQL, $db9568) or die(mysql_error());

$insertGoTo = “CRcart/directory_listing_pic.php”;
if (isset($_SERVER[‘QUERY_STRING’])) {
$insertGoTo .= (strpos($insertGoTo, ‘?’)) ? “&” : “?”;
$insertGoTo .= $_SERVER[‘QUERY_STRING’];
}
header(sprintf(“Location: %s”, $insertGoTo));
}

The above works perfectly on its own.

This is the coding, which also works perfectly on its own, for sending the e-mail:

$config = array(‘auth’ => ‘login’,
‘port’ => 25,
‘username’ => ‘xciting’,
‘password’ => ‘2217’);

$transport = new Zend_Mail_Transport_Smtp (‘smtp.xcitingideas.ca’, $config);

$mail = new Zend_Mail ();
$mail->setBodyText ('New directory listing entered in ’ . $_POST[‘breedName’]);
$mail->setFrom ($_POST[‘emailAddy’], $_POST[‘firstName’] . " " . $_POST[‘lastName’]);
$mail->addTo (‘webmaster@caninereview.ca’, ‘Helen W. Lee’);
$mail->setSubject (‘New Directory Listing entered’);
$mail->send($transport);

The problem is that when I insert the sending e-mail coding in among the first stuff, it breaks the $insertGoTo command and the user is staying on the order form page. I’ve tried putting the e-mail code before the $insertGoTo command, after the $insertGoTo command, before the mysql_select_db stuff (I think - I’m losing track of all my attempts) but no matter where I put it, the user doesn’t get sent to the “add a picture” page, although the e-mail does get sent.

Any suggestions? Or an alternative way of sending an e-mail to myself once the entry is inserted into the db? I can send all the coding if necessary - just didn’t want to make this post too long. :>)

The code should go just before the

header(sprintf(“Location: %s”, $insertGoTo));

Line.

If that’s not working, then it likely means you have already sent a header to the user.

Try this:

Add the code before, where I specified.

Then inbetween that code and header(), add this line:

if (headers_sent($filename,$line)) die(“Header was already sent at $filename:$line”);

Thanks wonshikee but it didn’t work either way. After just putting the e-mail coding in before the header(sprintf…) it sent the e-mail and entered the record into the db but stayed on the original form. After inserting the if (headers_sent…) coding, the form gave me the error: Header was already sent at /Applications/MAMP/htdocs/crtestingfolder/directory_listing_order-testemail.php:3 (I’m testing it in MAMP, so that I don’t mess up the live database - would that make a difference?) It DID enter the data into the database and send the e-mail, however; just didn’t give the user the “add a photo” page.

The section now reads:

$insertGoTo = “CRcart/directory_listing_pic.php”;
if (isset($_SERVER[‘QUERY_STRING’])) {
$insertGoTo .= (strpos($insertGoTo, ‘?’)) ? “&” : “?”;
$insertGoTo .= $_SERVER[‘QUERY_STRING’];
}

  $config = array('auth' => 'login',
'port' => 25,
'username' => 'xciting',
'password' => '2217');

$transport = new Zend_Mail_Transport_Smtp (‘smtp.xcitingideas.ca’, $config);

$mail = new Zend_Mail ();
$mail->setBodyText ('New directory listing entered in ’ . $_POST[‘breedName’]);
$mail->setFrom ($_POST[‘emailAddy’], $_POST[‘firstName’] . " " . $_POST[‘lastName’]);
$mail->addTo (‘webmaster@caninereview.ca’, ‘Helen W. Lee’);
$mail->setSubject (‘New Directory Listing entered’);
$mail->send($transport);

if (headers_sent($filename,$line)) die(“Header was already sent at $filename:$line”);

header(sprintf(“Location: %s”, $insertGoTo));

You need to open up the file /Applications/MAMP/htdocs/crtestingfolder/directory_listing_order-testemail.php.

Then you need to look at line 3 and see what is causing the output.

Already did that but didn’t think of it until after I hit send on my previous post. :>)

line 3: <?php
line 4: require_once (‘Zend/Mail.php’);
line 5: require_once (‘Zend/Mail/Transport/Smtp.php’);
line 6: ?>

That’s the coding which interprets the e-mail coding

And is there any white space before the <?php ?

The fact that <?php is at line 3 makes me think that the first two lines are empty (which is sent to the browser as empty HTML), which will cause any header redirection to fail.

THANK YOU!!! That was the problem. Line 1 is the php Connections code line, then there was a blank line. I tend to separate blocks of code out so it’s easy for me to find but obviously that only works in the body section, not the head. Another lesson my tired old brain has to try and retain. Thanks so much wonshikee!! The form works perfectly now.