<?php
while ($row = mysql_fetch_array($result)){
if ($_POST['text']==""){
?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ;?>">
<input type='text' size=20 name='sendingto' value="<?php echo $row[email];?>" />
<input type='text' name='text' size=20><br /><br />
<input type='text' name='comments' size=20><br /><br />
<input type='submit' name='submit' value='Submit Form' /></form>
<br /><br />
<?php
}
else{
$mailto = $_POST['sendingto'] ;
$subject = 'Email Subject' ;
$text = $_POST['text'] ;
$comments= $_POST['comments'] ;
$uself = 0;
$sep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$messageproper = "Email body.\n";
mail($mailto, $subject, $messageproper, "From: \"$text\" <$text>" . $sep . "Reply-To: \"$text\" <$text>" . $sep);
echo "email sent";
}
?>
i took out the trailing "}}", as i think it was a copied by accident.
first off an html issue: you mix no, single and double quotes in tags. use double quotes! i.e.
<input type="text" size="20" name="sendingto" value="<?php echo $row[email];?>" />
instead of
<input type='text' size=20 name='sendingto' value="<?php echo $row[email];?>" />
there is a similar inconsistency in the PHP code. i would use
mail($mailto, $subject, $messageproper, "From: " . $text . " <" . $text . ">" . $sep . "Reply-To: " . $text . " <" . $text . ">" . $sep);
instead of
mail($mailto, $subject, $messageproper, "From: \"$text\" <$text>" . $sep . "Reply-To: \"$text\" <$text>" . $sep);
that is, in case that $text is an email address! otherwise it will cause an error, when it's between the "<" and ">".
i'm really just taking a wild stab at it, but this is how i would write the code (though i personally would not mix html and php; but this - just like the above issues - is mostly personal preference and style). hopefully the changes in the form values address the problem you meant!
<?php
//stuff here
while ($row = mysql_fetch_array($result)){
if (!$_POST['text']){
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']; ?>">
<input type="text" size="20" name="sendingto" value="<?=$row[email]; ?>" />
<input type="text" name="text" size="20" value="<?=htmlspecialchars(trim($_POST['text'])); ?>" /><br /><br />
<input type="text" name="comments" size="20" value="<?=htmlspecialchars(trim($_POST['comments'])); ?>" /><br /><br />
<input type="submit" name="submit" value="Submit Form" />
</form>
<br /><br />
<?php
}
else{
$uself = false;
$sep = (!isset($uself) || ($uself == 0) ?
"\r\n" :
"\n");
if (mail(
$_POST['sendingto'],
"Email Subject",
"Email body.\n" . $_POST['comments'],
"From: " . $_POST['text'] . $sep . "Reply-To: " . $_POST['text'] . $sep
)
) {
echo "email sent";
}
else {
echo "ERROR!";
}
}
//stuff here
?>
to prevent errors in the mail() execution, i would advise a regex-check for the validity of the email address!