Dynadot โ€” .com Transfer

Form Mail Script Doesn't Work

SpaceshipSpaceship
Watch

abcde

Established Member
Impact
16
This script allows people to send me email. But it always get error like this:

Code:
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/******/public_html/mail.php:2) in /home/******/public_html/mail.php on line 4
and i dont even know what it means.

here is the code:
PHP:
<?php 
if ($_POST['action'] == "go") { 
  session_start(); 
  $name = $_POST['name']; 
  $email = $_POST['email']; 
  $content = $_POST['content']; 
  if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i", $email)) $alert = "You have entered an invalid email address."; 
  if ($name == "" OR $email == "" OR $content == "") $alert = "To send a message, please complete all 3 fields."; 
  if ($_SESSION['mail_count'] >= "3") $alert = "Only 3 messages can be sent per session."; 
  if (!$alert) { 
    if (!isset($_SESSION['mail_count'])) $_SESSION['mail_count'] = 0; 
    $_SESSION['mail_count']++; 
    $message .= "Name as follows:\n\n"; 
    $message .= "$name\n\n"; 
    $message .= "Email address as follows:\n\n"; 
    $message .= "$email\n\n"; 
    $message .= "Message as follows:\n\n"; 
    $message .= "$content\n\n"; 
    mail("[email protected]", "yourdomain.com Message" , "$message", "From: Website <>");
    $name = ""; 
    $email = ""; 
    $content = ""; 
    $alert = "Your message has been sent."; 
  } 
} 
?> 
<html> 
<head> 
</head> 
<body> 
<table align=center cellspacing="0" cellpadding="5" border="1" style="height: 450px; width: 500px; margin-top: 50; border-style:inset;"> 
<tr><td align="center"><form method=post action="mail.php"><br> 
Send me a message<br><br> 
your name<br><input type=text style="width: 330px;" name=name value="<?=$name?>" maxlength=50><br><br> 
your email address<br><input type=text style="width: 330px;" name=email value="<?=$email?>" maxlength=50><br><br> 
your message<br><textarea name="content" rows="6" cols="40"><?=$content?></textarea> 
<br><br> 
<input type="submit" name="submit" value="submit"> 
<input type="hidden" name="action" value="go"> 
</form></td></tr> 
</table> 
<?php 
if ($alert) { 
echo "<script type='text/javascript'> 
<!-- 
alert ('$alert'); 
//--> 
</script>"; 
} 
?> 
</body></html>
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
You probably just need to move session_start() to to the top of the file and outside of the if block.
 
0
•••
Dont know if this will work, try this

Code:
<?php
  session_start();
if ($_POST['action'] == "go") {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $content = $_POST['content'];
  if (!preg_match("(^[-\w\.]+@([-a-z0-9]+\.)+[a-z]{2,4}$)i", $email)) $alert = "You have entered an invalid email address.";
  if ($name == "" OR $email == "" OR $content == "") $alert = "To send a message, please complete all 3 fields.";
  if ($_SESSION['mail_count'] >= "3") $alert = "Only 3 messages can be sent per session.";
  if (!$alert) {
    if (!isset($_SESSION['mail_count'])) $_SESSION['mail_count'] = 0;
    $_SESSION['mail_count']++;
    $message .= "Name as follows:\n\n";
    $message .= "$name\n\n";
    $message .= "Email address as follows:\n\n";
    $message .= "$email\n\n";
    $message .= "Message as follows:\n\n";
    $message .= "$content\n\n";
    mail("[email protected]", "yourdomain.com Message" , "$message", "From: Website <>");
    $name = "";
    $email = "";
    $content = "";
    $alert = "Your message has been sent.";
  }
}
?>
<html>
<head>
</head>
<body>
<table align=center cellspacing="0" cellpadding="5" border="1" style="height: 450px; width: 500px; margin-top: 50; border-style:inset;">
<tr><td align="center"><form method=post action="mail.php"><br>
Send me a message<br><br>
your name<br><input type=text style="width: 330px;" name=name value="<?=$name?>" maxlength=50><br><br>
your email address<br><input type=text style="width: 330px;" name=email value="<?=$email?>" maxlength=50><br><br>
your message<br><textarea name="content" rows="6" cols="40"><?=$content?></textarea>
<br><br>
<input type="submit" name="submit" value="submit">
<input type="hidden" name="action" value="go">
</form></td></tr>
</table>
<?php
if ($alert) {
echo "<script type='text/javascript'>
<!--
alert ('$alert');
//-->
</script>";
}
?>
</body></html>

PJ
 
0
•••
yes... session_start MUST be the first line of code. (apparantly)
 
0
•••
No, it doesn't matter where the session_start(); is, as long as there is no output before it. It's the same with headers (header("Location: http://www.namepros.com/"); for example).

Check there isn't a space or extra line before the <?php.
 
0
•••
just as a matter of interest... what does this do:
PHP:
<?php 
if ($alert) { 
echo "<script type='text/javascript'> 
<!-- 
alert ('$alert'); 
//--> 
</script>"; 
} 
?>
that this won't:
PHP:
<?php 
if ($alert) { 
echo "<script type='text/javascript'> 
alert ('$alert'); 
</script>"; 
} 
?>

What I want to know is, why put in <!-- and //--> ??? I have seen it a lot in phpbb too.
 
0
•••
<!-- --> are used to denote a comment in HTML. Placing them around what was inside the script tags was mostly for older browsers who did not understand the script tag. This kept the scripting from being displayed on the page as text in those cases.
 
0
•••
Try this if ur looking 4 a simple mailform

<html>
<head>
<?

// your name
$recipientname = "name";

// your email
$recipientemail = "email";

// subject of the email sent to you
$subject = "response form ";

// thankyou displayed after the user clicks "submit"
$submitmessage = "Thank You For Your Comment";
?>
</head>
<body>

<?

if($submitform) {

// check required fields
$dcheck = explode(",",$require);
while(list($check) = each($dcheck)) {
if(!$$dcheck[$check]) {
$error .= "Missing $dcheck[$check]<br>";
}
}

// check email address
if( !eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*"
."@([a-z0-9]+([\.-][a-z0-9]{1,})+)*$",
$Email, $regs) )
{


$error .= "'$Email' isn't a valid mail address!<BR>";


}
elseif( gethostbyname($regs[2]) == $regs[2] )
{
$error .= "Error: Can't find the host '$regs[2]'!<br>";
}

// display errors
if($error) {
?>

<b>Error</b><br>
<? echo $error; ?><br>
<a href="#" onClick="history.go(-1)">try again</a><BR><BR>
<?
}else{

// format message
$message = "Response for $recipientname:
<html>
<table width=\"600\" border=\"0\" cellpadding=\"2\">
<tr>
<td width=\"175\"><b>Full Name:</b></td>
<td width=\"400\">$Name</td>
</tr>
<tr>
<td width=\"175\"><b>Email:</b></td>
<td width=\"400\">$Email</td>
</tr>
<tr>
<td colspan=\"2\"><hr width=\"600\"></td>
</tr>
<tr>
<td width=\"175\"><b>Comments:</b></td>
<td width=\"400\">$Comments</td>
</tr>
</table>
</body>
</html>";

// send mail and print success message
mail($recipientemail,"$subject","$message","From: $Name <$Email>");


echo "$submitmessage";
}
}
if(!$submitform) {
?>

<div align="right"></div><br><br>
<form name="contactform" action="index.php?section=mailform" method="post">
<input type="hidden" name="require" value="Name,Email,Comments">
<table border="0" width="0" align="left">
<tr align="left">
<td width="100" valign="top">Full Name</td>
<td width="0" valign="top"><div class="red" align="left">*</div></td>
<td width="0" valign="top"><input name="Name" size="32"></td>
</tr>
<tr align="left">
<td valign="top">E-mail</td>
<td valign="top" class="red" >*</td>
<td valign="top"><input name="Email" size="32"></td>
</tr>
<tr align="left">
<td valign="top">Comments</td>
<td valign="top" class="red" >*</td>
<td valign="top"><textarea name="Comments" rows="5" cols="54"></textarea></td>
</tr><tr align="left">
<td colspan="3" align="left"><input type="submit" id="submit" value="Submit" name="submitform"></td>
</tr></table>
<? }
?>
</form>
</body>
</html>
 
0
•••
Appraise.net
Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomainEasy โ€” Payment Flexibility
DomDB
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back