Jump to content

Recommended Posts

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!

[code]
<?
$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;

?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/5651-php-form-only-returns-first-line/
Share on other sites

Why are you using the strtok() function on these lines?
[code]<?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" );
?>[/code]

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.