Jump to content


Photo

PHP Form only returns first line


  • Please log in to reply
2 replies to this topic

#1 outsidaz

outsidaz
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 March 2006 - 01:50 AM

I added a PHP Mail contact form on my site and everything works fine except for my comment field. Whenever the user adds a carriage return in the field, only the first line of the comment variable gets returned. Is there any way to prevent the user from entering a carriage return in the form since I have heard that carriage returns are bad. Or, is there a way to allow carriage returns in this variable? Any help would be appreciated. Thanks!

<?
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$compname = $_POST['compname'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['email'])) {
    header( "Location: $formurl" );
    exit;
}
if (empty($firstname) || empty($lastname) || empty($compname) || empty($address1) || empty($city) || empty($state) || empty($zip) || empty($country) || empty($phone) || empty($email) || empty($comments)) {
   header( "Location: $errorurl" );
   exit;
}
$date = date('l dS \ F Y h:i:s A');
$firstname = strtok( $firstname, "\r\n" );
$lastname = strtok( $lastname, "\r\n" );
$compname = strtok( $compname, "\r\n" );
$address1 = strtok( $address1, "\r\n" );
$address2 = strtok( $address2, "\r\n" );
$city = strtok( $city, "\r\n" );
$state = strtok( $state, "\r\n" );
$zip = strtok( $zip, "\r\n" );
$country = strtok( $country, "\r\n" );
$phone = strtok( $phone, "\r\n" );
$fax = strtok( $fax, "\r\n" );
$email = strtok( $email, "\r\n" );
$comments = strtok( $comments, "\r\n" );
$referral = strtok( $referral, "\r\n" );
if (get_magic_quotes_gpc()) {
    $comments = stripslashes( $comments );

}

$messageproper =

    "This message was sent from: " .
    "$firstname " .
    "$lastname\n" .
    " at " .
    "$date\n" .
    "\n" .
    "$firstname " .
    "$lastname\n" .
    "$compname\n" .
    "$address1\n" .
    "$address2\n" .
    "$city " .
    "," .
    "$state " .
    "$zip\n" .
    "$country\n" .
    "$phone\n" .
    "Fax: " .
    "$fax\n" .
    "$email\n" .
    "\n" .
    "How did you hear about us? " .
    "$referral\n" .
    "\n" .
    "Comments: " .
    "$comments\n";

mail($mailto, $subject, $messageproper, "From: \"$firstname $lastname\" <$email>\r\nReply-To: \"$firstname $lastname\" <$email>\r\nX-Mailer: chfeedback.php 2.04" );
header( "Location: $thankyouurl" );
exit;

?>


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 March 2006 - 03:43 AM

Why are you using the strtok() function on these lines?
<?php
$firstname = strtok( $firstname, "\r\n" );
$lastname = strtok( $lastname, "\r\n" );
$compname = strtok( $compname, "\r\n" );
$address1 = strtok( $address1, "\r\n" );
$address2 = strtok( $address2, "\r\n" );
$city = strtok( $city, "\r\n" );
$state = strtok( $state, "\r\n" );
$zip = strtok( $zip, "\r\n" );
$country = strtok( $country, "\r\n" );
$phone = strtok( $phone, "\r\n" );
$fax = strtok( $fax, "\r\n" );
$email = strtok( $email, "\r\n" );
$comments = strtok( $comments, "\r\n" );
$referral = strtok( $referral, "\r\n" );
?>

That is why you're only getting the first line of your comments. The strtok() fuction splits a string (str) into smaller strings (tokens), with each token being delimited by any character from token. (taken from the PHP manual) You're telling it to get the first string until the first "\r\n" or new-line character. The newline character is sent when someone presses the return key.

Don't use this function at all any you will be fine.

Ken

#3 outsidaz

outsidaz
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 24 March 2006 - 06:33 PM

Thanks, Ken! I copied the script off of some site so I wasn't really sure what the strok was for. Everything works like a charm now.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users